如何使用MVC绑定DropDownList中的值? [英] How do you bind the value in DropDownList using MVC?
本文介绍了如何使用MVC绑定DropDownList中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用MVC绑定DropDownList中的值?
How do you bind the value in DropDownList using MVC?
推荐答案
看看这个
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/[^]
这篇关于如何使用MVC绑定DropDownList中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文