asp.net-mvc-4editorformodel

@HTML.editorForModel() doesn't create editing interface for Model


I have a model: Menu is subClass of menuComponent

and menu component has two properties: Name and Description both are string

I have created an Edit View and tried to create editing fields with editorForModel(). but it doesn't create any editing fields for it.

@using (Html.BeginForm())
{ 
    @Html.EditorForModel();
    <input type="submit" value="Save" /> 
    @Html.ActionLink("Cancel and return to List", "Index") 
}

I have to use @HTML.EditorFor() to create editing fileds

 @using (Html.BeginForm())
    { 
        @Html.EditorFor(m => m.Name);
        @Html.EditorFor(m => m.Description);
        <input type="submit" value="Save" /> 
        @Html.ActionLink("Cancel and return to List", "Index") 
    }

why this happen?


Solution

  • That's a bug that I reported to Microsoft: http://connect.microsoft.com/VisualStudio/feedback/details/636341/modelmetadata-fromlambdaexpression-has-changed-in-asp-net-mvc-3-rtm

    Basically Editor and Display templates only look for properties on the exact current type of your model, not in parent classes.

    Their official answer is this:

    Hi Darin (and others),

    This was a deliberate change that we introduced to ASP.NET MVC 3 that was the result of a trade-off between having better support for inherited models or better support for models that implement interfaces. We ended up favoring inherited models, which from our experience is a more common approach.

    The fundamental problem is that when an interface is implemented by a class that the class doesn't really inherit any of the members of the interface. The key here is that because it doesn't inherit the members of the interface, it also does not inherit the metadata on those members.

    Thanks, The ASP.NET Team

    So basically they do not consider it as a bug but it is by design.