C# 使用MVC从数据库填充选择框
我正在使用asp.net和MVC创建一个网站。我需要用数据库中的值填充一个选择框,我想知道我该怎么做?有很多不同的方法,但我就是这么做的。在我的服务类中,我执行db select并返回SelectList对象C# 使用MVC从数据库填充选择框,c#,html,asp.net,asp.net-mvc,C#,Html,Asp.net,Asp.net Mvc,我正在使用asp.net和MVC创建一个网站。我需要用数据库中的值填充一个选择框,我想知道我该怎么做?有很多不同的方法,但我就是这么做的。在我的服务类中,我执行db select并返回SelectList对象 public SelectList GetAsSelectList() { var depts = from d in GetAll() select new
public SelectList GetAsSelectList()
{
var depts = from d in GetAll()
select new
{
Id = d.Id,
Name = d.Name
};
return new SelectList(depts, "Id", "Name");
}
然后在我的模型中,我将selectlist指定给属性:
model.Suppliers = _supplierService.GetAsSelectList();
return View(model);
最后一个观点是:
@Html.DropDownListFor(model => model.DeliveryStoreId, Model.DeliveryStores)
你能做点什么吗?我只是想找一些指导,因为我对使用asp.net和mvcUse
Html.DropDownListFor()
还不熟悉。谢谢,这对我来说很有用