为什么我会收到“错误”消息“选择”在VS 2010中无法解决
我在 MVC 应用程序中使用 Html.DropDownListFor 来允许用户选择类别。我通过对视图模型中的对象集合使用 Linq 的 Select 方法来动态创建 SelectList。这是代码:
<%: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();"})%>
它可以工作,但我想知道为什么代码完成不起作用以及为什么 intelliscense 没有为我的类别对象列表选择 Select 方法。当我运行该页面时,没有任何错误,但我想知道为什么 Visual Studio 认为代码存在错误。
顺便说一句,这比我在视图中想要的代码要多一些——将 SelectList 作为我的视图模型的一部分包含在内是否有意义?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您的视图模型应该包含 SelectListItem 列表本身;它不应该从视图中派生。听起来您的视图中没有引用 System.Linq;检查您的视图级别 web.config 并查看文件中是否有以下命名空间:
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: