0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.