Reputation: 580
Some background info, I'm using MVC3 w/ EF & db first method, along with Razor templating. I'm passing in a view model to a view, that view model has a couple different collections of obj, i'm using one of them to populate a webgrid (wgSettings) as its source parameter. Below is the code in which i'm trying to get some detailed information about a related row. Details: MyInfo is the Dictionary from the viewmodel, item is the anonymous row object that the webgrid uses to populate the row item.Value.Value is the value of the id field of the object from the dictionary that I want to grab MySettings is a System.Data.Objects.DataClasses.EntityCollection, therefore, I can use extension methods like Single(), FirstOrDefault(), etc.
using quick watch I am able to navigate the object and see the values I would expect, however when debugging, and accessing the view @ runtime, I am getting the following error.
On line: "@Model.MyInfo[Guid.Parse(item.Value.Value)].MySettings.Single().Value" debugger is saying 'System.Data.Objects.DataClasses.EntityCollection' does not contain a definition for 'Single' but when I quickwatch it i am getting value.
Below is the code.
wgSettings.Column(header: "Value", columnName:"Value", canSort:true, format:
@<text>
@if (item.Value.Type == "Guid" && item.Value.Value != null) {
@Model.MyInfo[Guid.Parse(item.Value.Value)].MySettings.Single().Value
}
else if (item.Value.Type == "Boolean") {
@item.Value.Value
}
else {
@item.Value.Value
}
</text>
Upvotes: 2
Views: 1152
Reputation: 7584
The Linq extension methods are not in scope, you need to add this to the top (or you can add to web.config)
@using System.Linq;
Or in the web.config in the views folder:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Linq" />
</namespaces>
</pages>
</system.web.webPages.razor>
See this question for a similar problem for a different namespace:
Using System.Data.Linq in a Razor view
Upvotes: 2