.NET MVC,SelectLists和LINQ [英] .net MVC, SelectLists, and LINQ

查看:146
本文介绍了.NET MVC,SelectLists和LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来使用MVC框架的Html.DropDownList和我有一个很难understading如何选择数据出来我的数据库绑定到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?

推荐答案

选择列表的构造函数一个IEnumerable因此,所有你需要做的就是通过LINQ查询,像这样的构造

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.

这篇关于.NET MVC,SelectLists和LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆