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

Community
  • 1
  • 1
DeeRain
  • 1,344
  • 1
  • 15
  • 24

2 Answers2

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

Community
  • 1
  • 1
Ian Routledge
  • 4,012
  • 1
  • 23
  • 28
  • If it's something you do often, then put it into an HTML helper, then it will make it beautiful :) – Ian Routledge Sep 03 '12 at 14:09
  • 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. – DeeRain Sep 03 '12 at 14:13
  • 1
    Your're right, it is a bug in MVC, see http://connect.microsoft.com/VisualStudio/feedback/details/654543/asp-net-mvc-possible-mvc-bug-when-working-with-editortemplates-and-drop-down-lists#details - the solution above is a workaround. Looks like it's fixed in MVC4. – Ian Routledge Sep 03 '12 at 14:25
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")
BigMike
  • 6,683
  • 1
  • 23
  • 24
  • sorry, mistype. In InnerModel editor @Html.DropDownListFor(m=>m.Value,items,"no_selected"). Correct question. – DeeRain Sep 03 '12 at 13:53