且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用MVC绑定DropDownList中的值?

更新时间:2022-02-06 02:28:52

看看这个

mvc dropdownlist binding [ ^ ]


1。我们在BindingDropDown方法中创建了一个 SelectListItems 列表,并将其传递给View。把它扔到了View。



1. We have created a list of SelectListItems inside the "BindingDropDown" method and passed it to the View. ssed it to the View.

List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem
{
    Text = "item1",
    Value = "1"               
});
items.Add(new SelectListItem
{
    Text = "item2",
    Value = "2"                
});

ViewData["ListItems"] = items;//This will be used to bind to Dropdownlist





绑定下拉视图



a。我们可以使用html.Dropdownlist绑定下拉列表(它是一个HTML帮助类)。





Binding Drop down at View

a. We can bind the dropdown list using html.Dropdownlist (it is a HTML helper class).

ListItems: <%= Html.DropDownList("ListItems")%>





这里我们传递从控制器传递到视图的ListItems。



b。 html.DropdownList类的另一个重载方法,用于绑定下拉列表。





Here we are passing "ListItems" that has been passed from the controller to the view.

b. Another overloaded method of the html.DropdownList class for binding a dropdown list.

<%= Html.DropDownList("SelectedItem", (IEnumerable<SelectListItem>)ViewData["ListItems"])%>





运行后,您将获得包含2个项目的下拉列表



After run, u will get dropdown list with 2 items


请参阅下面的链接以获取下拉列表



http ://forums.asp.net/t/1946767.aspx?MVC + 4 +如何+ +填充+下降+下降+值+使用+ html + dropdownlistfor [ ^ ]



http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in- mvc-4-using-razor / [ ^ ]



http://agilewarrior.wordpress.com/2012/12/13/how-to-simple-html-dropdownlistfor- mvc-net / [ ^ ]
see below link for drop down list

http://forums.asp.net/t/1946767.aspx?MVC+4+how+to+populate+drop+down+value+using+html+dropdownlistfor[^]

http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/[^]

http://agilewarrior.wordpress.com/2012/12/13/how-to-simple-html-dropdownlistfor-mvc-net/[^]