選択リストのデフォルト値を設定する次のコードがあります。
public ActionResult Register()
{
IList<Country> countryList = _countryRepository.GetAllCountry();
var registerViewModel = new RegisterViewModel
{
CountryId = 52,
CountryList = new SelectList(countryList, "CountryId", "CountryName", "Select Country")
};
return View(registerViewModel);
}
私の見解ではこれがあり、これはうまく機能し、選択した国の値を 52 に設定します。
<%: Html.DropDownListFor(model => model.CountryId, Model.CountryList ,"Select Country") %>
ただし、このためのエディター テンプレートを作成すると、国のデフォルト値が選択されていません
したがって、現在のビューを次のように変更します。
<%: Html.EditorFor(model => model.CountryId,new { countries = Model.CountryList}) %>
次に、次のようなエディター テンプレートを作成します。
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Int64?>" %>
<%= Html.DropDownList(
String.Empty /* */,
(SelectList)ViewData["countries"],
"Select Country"
)
%>