新しいモデルをビューモデルに追加して、国のリストを保存します。このプロパティのタイプはSelectListItem
のコレクションにすることができます。
public class CreateSomething
{
public List<SelectListItem> Countries { set;get;}
//Your existing properties
public string CountryCode { set;get; }
}
GETアクションでは、このプロパティにデータをロードするだけです。
public ActionResult Create()
{
var vm= new CreateSomething();
vm.Countries= new List<SelectListItem> {
new SelectListItem { Value="USA", Text="USA"},
new SelectListItem { Value="CANADA", Text="Canada"}
};
return View(vm);
}
とあなたのビューで
@model CreateSomething
@using(Html.BeginForm())
{
<label> Select a country </label>
@Html.DropDownListFor(m => m.CountryCode,Model.Countries)
<input type="submit" />
}