1

I have simple test model:

  public class MyModel
    {
        public InnerModel InnerModel { get; set; }

    }

    public class InnerModel
    {
        public int Value { get; set; }
    }

In controller:

public ActionResult Index()
{
    var model = new MyModel();

    model.InnerModel = new InnerModel { Value = 3 };

    return View("MyModelView", model);
}

MyModelView:

@model MyModel

@{
    var items = new List<SelectListItem>()
                    {
                        new SelectListItem {Text = "one", Value = "1"},
                        new SelectListItem {Text = "two", Value = "2"},
                        new SelectListItem {Text = "three", Value = "3"}
                    }; }


@Html.DropDownListFor(m=>m.InnerModel.Value,items,"no_selected")

When page load i see selected item: enter image description here

It's good.

But if I add EditorTemplate InnerModel.cshtml:

@model InnerModel

    @{
        var items = new List<SelectListItem>()
                        {
                            new SelectListItem {Text = "one", Value = "1"},
                            new SelectListItem {Text = "two", Value = "2"},
                            new SelectListItem {Text = "three", Value = "3"}
                        }; }


    @Html.DropDownListFor(m=>m.Value,items,"no_selected")

And change MyModelView:

@model MyModel
@Html.EditorFor(m=>m.InnerModel,"InnerModel")

When page loaded i see: enter image description here

Why? MVC bug?

UPDATE: This real bug. See

0

2 Answers 2

8

Try this instead when creating the list of select items:

var items = new SelectList(
        new[] 
        {
            new { Value = "1", Text = "one" },
            new { Value = "2", Text = "two" },
            new { Value = "3", Text = "three" },
        }, 
        "Value", 
        "Text", 
        Model.Value
    )

Here is an explanation of why this happens: https://stackoverflow.com/a/11045737/486434

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

3 Comments

If it's something you do often, then put it into an HTML helper, then it will make it beautiful :)
Page you linked says "only if this lambda expression is a simple property access expression". My lambda is simple. I don't understand why it does not work.
Your're right, it is a bug in MVC, see connect.microsoft.com/VisualStudio/feedback/details/654543/… - the solution above is a workaround. Looks like it's fixed in MVC4.
1

with the @Model InnerModel change this

@Html.DropDownListFor(m=>m.InnerModel.Value,items,"no_selected")

to

@Html.DropDownListFor(m=>m.Value,items,"no_selected")

1 Comment

sorry, mistype. In InnerModel editor @Html.DropDownListFor(m=>m.Value,items,"no_selected"). Correct question.

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.