.net MVC,SelectLists和LINQ(.net MVC, SelectLists, and LINQ)

编程入门 行业动态 更新时间:2024-10-27 00:35:02
.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构造函数接受一个I​​Enumerable,所以你需要做的就是像这样将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.

更多推荐

本文发布于:2023-07-24 01:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MVC   net   LINQ   SelectLists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!