下面是一個在頁面上有7個下拉菜單的例子,每個下拉菜單都有相同的5個選項。每個下拉菜單都可以選擇不同的選項。
在我看來,我有我的表單中下面的代碼:
<%= Html.DropDownListFor(m => m.ValueForList1, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList2, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList3, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList4, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList5, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList6, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList7, Model.AllItems)%>
然後,我有一個這樣的視圖模型:
public class HomePageViewModel
{
public List<SelectListItem> AllItems { get; set; }
public string ValueForList1 { get; set; }
public string ValueForList2 { get; set; }
public string ValueForList3 { get; set; }
public string ValueForList4 { get; set; }
public string ValueForList5 { get; set; }
public string ValueForList6 { get; set; }
public string ValueForList7 { get; set; }
public HomePageViewModel()
{
AllItems = new List<SelectListItem>
{
new SelectListItem {Text = "First", Value = "First"},
new SelectListItem {Text = "Second", Value = "Second"},
new SelectListItem {Text = "Third", Value = "Third"},
new SelectListItem {Text = "Fourth", Value = "Fourth"},
new SelectListItem {Text = "Fifth", Value = "Fifth"},
};
}
}
在你的控制器方法
現在,聲明如下:
public ActionResult Submit(HomePageViewModel viewModel)
viewModel.ValueForList1的值將被設置爲選定的值。
當然,我建議使用某種類型的數據庫中的枚舉或ID作爲您的值。
來源
2010-04-15 06:58:27
Mac