I have a page contains a drop down list for a enum which is binding on a field in the model, also I have a url parameter have the same name(first letter's case is not same).
When the url parameter is empty, the binding for the enum is not correct. To clarify my question, please see the detail code below.
For example, when the url is Index?eventType=
, the view initially show Type1 instead of Type2. Is there any special rule for the MVC5 cause that weird behavior?
When I either change the url parameter name or the field name in the model, the drop down list could select Type2 when load Index?eventType=
View:
@model TestMode
@{
Layout = null;
}
@Html.EnumDropDownListFor(m => m.EventType)
Controller:
public ActionResult Index(int? eventType)
{
var viewModel = new TestMode();
viewModel.EventType = eventType.HasValue ?
(CalendarEventType)eventType.Value :
CalendarEventType.Type2;
//Check the value of EventType here is CalendarEventType.Type2, but when the view loaded, there are not selected for the drop down list of enum
return View(viewModel);
}
ViewModel:
public class TestMode
{
public CalendarEventType EventType { get;set;}
}
public enum CalendarEventType
{
Type1 = 0,
Type2 = 1,
}
HTML for the Index?eventType=
, there are no option be selected.
<select data-val="true" data-val-required="EventType field is required" id="EventType" name="EventType">
<option value="0">Type1</option>
<option value="1">Type2</option>
</select>