开发者

Why Do I Get An "Error" that "Select" Can Not be Resolved in VS 2010

I am using the Html.DropDownListFor in my MVC application to allow users to select a Category. I am creating the SelectList dynamically by using Linq's Select method on a collection of objects in my view model. Here is the code:

<%:Html.DropDownListFor(m => m.SelectedCategoryId, 
            Model.Categories.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString(), Selected = Model.SelectedCategoryId == c.Id}).ToList(),
            new{onchange = "this.form.submit();"})%>

It works but I'm wondering why code completion doesn't work and why intelliscense is not picking up the Select method for my List of Category 开发者_高级运维objects. When I run the page I have no errors, but I'm wondering why Visual Studio thinks there are errors with the code.

On a side note, this is a bit more code than I would like in a View-- does it make sense to include the SelectList as part of my view model?


Yes, your view model should contains the SelectListItem list itself; it shouldn't be derived on the view. It sounds like you don't have System.Linq referenced on your view; check your view level web.config and see if you have the following namespace in your file:

<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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜