.net MVC,SelectLists和LINQ(.net MVC, SelectLists, and LINQ)
我是在MVC框架中使用Html.DropDownList的新手,并且很难解释如何从我的数据库中选择数据绑定到DropDownList。 有没有简单的方法来从标准的LINQ查询中返回可绑定列表(如SelectList)?
I am new to using the Html.DropDownList in the MVC framework and am having a hard time understading how to select the data out my database to bind to the DropDownList. Is there an easy way to return a bindable list (such as a SelectList) from a standard LINQ query?
最满意答案
SelectList构造函数接受一个IEnumerable,所以你需要做的就是像这样将LINQ查询传递给构造函数
var query = from c in customers select c; var customerList = new SelectList(query, "CustomerId", "CustomerName");您应该在Controller中执行此操作,并在ViewModel中使用SelectList。
The SelectList constructor takes an IEnumerable so all you need to do is pass the LINQ query to the constructor like so
var query = from c in customers select c; var customerList = new SelectList(query, "CustomerId", "CustomerName");You should do this in the Controller and have the SelectList in your ViewModel.
更多推荐
发布评论