私はモデルを持っています、
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: 顧客タイプの model[i] => を使用しています。簡単にするために、html タグをレンダリングする前に forloop を削除しました。
@using(Html.BeginForm())
{
for(int i = 0; i < Model.Count(); i++)
{
@Html.TextBoxFor(model => model[i].Name)
@Html.DropDownListFor..........
}
}