I have this code to set the default value for my select list:
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);
}
I have this in my opinion and it works well for the selected value of country 52:
<%: Html.DropDownListFor(model => model.CountryId, Model.CountryList ,"Select Country") %>
However, when I create an editor template for this, the default value for the country is not selected
So, I am changing my current view to the following:
<%: Html.EditorFor(model => model.CountryId,new { countries = Model.CountryList}) %>
Then I create my editor template as follows:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Int64?>" %>
<%= Html.DropDownList(
String.Empty ,
(SelectList)ViewData["countries"],
"Select Country"
)
%>
source
share