我有一个模型,
public class Customer
{
public string Name { get; set;}
public string CountryCode { get; set;}
}
在控制器中
var model = new List<Customer>
{
new Customer { Name = "foo", CountryCode = "US"},
new Customer { Name = "bar", CountryCode = "UK",
};
return PartialView("_Edit", model);
显示所有国家的扩展方法:-
public class CountryList
{
public static IEnumerable<SelectListItem> CountrySelectList
{
get
{
var list = new List<SelectListItem>()
{
new SelectListItem { Value = "US", Text="US" },
new SelectListItem { Value = "UK", Text="UK" },
};
return list;
}
}
}
在部分视图中
@model List<Customer>
@Html.DropDownListFor(model => model[i].CountryCode, CountryList.CountrySelectList, "Select Country Type")
但是下拉菜单没有选择每个客户的国家代码?有什么想法吗?
PS:它使用的是 Customer 类型的 model[i] =>,为简单起见,我在渲染 html 标签之前删除了 forloop。
@using(Html.BeginForm())
{
for(int i = 0; i < Model.Count(); i++)
{
@Html.TextBoxFor(model => model[i].Name)
@Html.DropDownListFor..........
}
}