我有这段代码来设置我的选择列表的默认值:
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"
)
%>