Reputation: 559
I have an editor template with a dropdown:
@Html.DropDownListFor(m => m.AdmissionState, new List<SelectListItem>()
{
new SelectListItem()
{
Text = "1",
Value = "1"
},
new SelectListItem()
{
Text = "2",
Value = "2"
},
new SelectListItem()
{
Text = "3",
Value = "3"
},
}, "- Select -")
I call it from the view:
@Html.EditorFor(m => m.Licenses)
In controller I have following code:
profile.Licenses.Add(new AttorneyLicenseModel()
{
AdmissionDate = "06/14/2012",
AdmissionState = "2",
AdmissionNumber = "asd",
});
profile.Licenses.Add(new AttorneyLicenseModel()
{
AdmissionDate = "07/16/2012",
AdmissionState = "5",
AdmissionNumber = "qwe",
});
For each of the licenses I see the fields with a data populated from the model, but not for the dropdown list - there is default value is checked. What am I doing wrong? ofcourse, the code with values is extremely simlified
Upvotes: 0
Views: 783
Reputation: 1038710
Try like this:
@Html.DropDownListFor(
m => m.AdmissionState,
new SelectList(
new[]
{
new { Value = "1", Text = "1" },
new { Value = "2", Text = "2" },
new { Value = "3", Text = "3" },
},
"Value",
"Text",
Model.AdmissionState
),
"- Select -"
)
And if you are interested in understanding why my code works and yours not you could read the following answer I provided yesterday to a similar question: ASP.NET MVC DropDownListFor not selecting value from model
Upvotes: 1