Asp.net 如何从数据库将数字绑定到Dropdownlist

Asp.net 如何从数据库将数字绑定到Dropdownlist,asp.net,asp.net-mvc-4,Asp.net,Asp.net Mvc 4,如何将数字从数据库绑定到Dropdownlist? 我想约束10,20,30…..直到2000年之后,我的要求可能会更改为5,10,15,20-2000, 我在数据库中有一列应该绑定到dropdownlist。我需要一个公共逻辑来绑定dropdownlist项 请帮我解决这个问题 创建您的模型: public class myViewModel{ public int selectedIndex { get; set; } //one for selected index pub

如何将数字从数据库绑定到Dropdownlist? 我想约束10,20,30…..直到2000年之后,我的要求可能会更改为5,10,15,20-2000, 我在数据库中有一列应该绑定到dropdownlist。我需要一个公共逻辑来绑定dropdownlist项 请帮我解决这个问题

创建您的模型:

public class myViewModel{
    public int selectedIndex { get; set; } //one for selected index
    public List<myItemsModel> mylist { get; set; } // one for list
}
public ActionResult myView() {
    myViewModel model = new myViewModel();
    model.selectedIndex  = aninteger;
    model.mylist = (from blah in blahblah .....);
    return View(model);
}
在你看来:

//Bind the model
@model myProject.Models.myViewModel

//Bind the 2 view models to the control
@Html.DropDownListFor(x => x.selectedIndex , new SelectList(Model.mylist, "id", "dt", Model.selectedIndex), "Placeholder", new { onchange = @"this.form.submit();", @class = "your css class" })

你想要数据生成逻辑还是绑定逻辑?我需要绑定逻辑…你试过这个吗