0

I am trying to grab each key value from a LINQ Query and pull them into my view. The LINQ query looks like this:

Public Property ByVDN As IEnumerable
    Get
        Dim valQ = (From scr In Var.db.CareSideA.ScriptCrossReferences
                   Join s In Var.db.CareSideA.Scripts On s.ScriptID Equals scr.ScriptID
                   Join ms In Var.db.CareSideA.MasterScripts On s.MasterScriptID Equals ms.MasterScriptID
                   Join svce In Var.db.CareSideA.Services On svce.SkillTargetID Equals scr.ForeignKey
                   Join p In Var.db.CareSideA.Peripherals On svce.PeripheralID Equals p.PeripheralID
                   Join sm In Var.db.CareSideA.ServiceMembers On svce.SkillTargetID Equals sm.ServiceSkillTargetID
                   Join sg In Var.db.CareSideA.SkillGroups On sm.SkillGroupSkillTargetID Equals sg.SkillTargetID
                   Where s.Version = ms.CurrentVersion And scr.TargetType = 1 And svce.PeripheralNumber = Value
                   Select New With {Key .Service = svce.PeripheralNumber,
                                    Key .ScriptName = ms.EnterpriseName,
                                    Key .Peripheral = p.EnterpriseName,
                                    Key .SkillMapping = sg.PeripheralNumber,
                                    Key .LatestVersion = s.Version,
                                    Key .Created = s.DateTime,
                                    Key .Author = s.Author}).ToList
        Return valQ
    End Get
    Set(value As IEnumerable)
    End Set
End Property

Now this does return results but they look like this: Screen Shot

Ideally I'd like to be able to do this:

<table>
For Each Item In Model.ByVDN
  Dim i = Item
    <tr>
      <td>@Html.DisplayFor(Function(m) i.Service)</td>
      <td>@Html.DisplayFor(Function(m) i.ScriptName)</td>
      <td>@Html.DisplayFor(Function(m) i.Peripheral)</td>
   Next

etc...

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
aaronmallen
  • 1,418
  • 1
  • 12
  • 29

1 Answers1

1

You can't pass about anonymous objects. Well, you can, but it is not strongly typed. You'll have to define a class with these properties, create IEnumerable of instances of that class and pass that Enumerable to the view. There is no other way.

UPD: see similar question: passing linq select query to the method

Community
  • 1
  • 1
trailmax
  • 34,305
  • 22
  • 140
  • 234
  • 1
    Over last 2 years of working with MVC I've came to conclusion that it is worth the effort to create a view model per view. Especially if the application does something non-basic and you plan to develop it further. – trailmax Dec 20 '13 at 00:27
  • I couldn't agree more. That's exactly what I would be doing, however we're on a code freeze until Feb. And I can't push the new view to our SQL servers until then "/ – aaronmallen Dec 20 '13 at 02:42
  • How C# classes for view models are connected to your SQL Server views? What is your workflow? – trailmax Dec 20 '13 at 12:42
  • unfortunately I'm having to follow a database first approach. My company had a .NET developer who learned how to develop asp.net forms while he worked here, did a crap job, demanded a $10000 raise and left when he didn't get his way. I was hired to clean up the mess. Also everything has to be written in vb.net not c# unfortunately. – aaronmallen Dec 20 '13 at 17:57
  • For EF Code First you don't need to create a SQL view to have a view model, either in C# or VB – trailmax Dec 20 '13 at 18:07
  • can you hit me with a link or some keywords to search on for how to do what your saying? – aaronmallen Dec 20 '13 at 21:00
  • 1
    I think you need this article http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/ - there will be plenty of info and keywords for further research. Jimmy Bogard has a good blog about MVC - worth reading every single post. – trailmax Dec 21 '13 at 02:18
  • hm... also this one (probably I'd start with this one, then the one above): http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/ – trailmax Dec 21 '13 at 02:20