1

I have an MVC model with a property that contains a generic collection of types that inherit from a single type. It displays the editor exactly as I would expect, but when I post back the types of all the items are the base type. How do I get it to return the correct types?

Model...

public class PageEM {
    public long Id { get; set; }
    public virtual IList<FieldEM> Fields { get; set; }
}
public class FieldEM { // I'd really like this to be abstract.
    public long Id { get; set; }
    public string Caption { get; set; }
    public string Value { get; set; }
}
public class TextFieldEM : FieldEM {
}
public class CheckBoxFieldEM : FieldEM {
    public bool ValueData {
        get { return (bool)Value; }
        set { Value = (string)value; }
}

PageEM View...

@model PageEM
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Fields</legend>
        @Html.HiddenFor(m => m.Id)
        @Html.EditorFor(m => m.Fields)
    <input type="submit" value="Submit" title="Submit" />
    </fieldset>
}

TextFieldEM Editor...

@model TextFieldEM
<div>
    @Html.HiddenForFor(m => m.Id)
    <div>
        @Html.LabelFor(m => m.Value, Model.Caption)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(m => m.Value)
        @Html.ValidationMessageFor(m => m.Value)
    </div>
</div>

CheckBoxFieldEM Editor...

@model CheckBoxFieldEM
<div>
    @Html.HiddenForFor(m => m.Id)
    <div class="editor-field">
        @Html.EditorFor(m => m.DataValue)@Html.LabelFor(m => m.DataValue, Model.Caption, new { @class = "checkbox" })
    </div>
</div>

Controller...

public partial class PageController : Controller {
    public virtual ActionResult Edit() {
        PageEM em = new PageEM() {
            Id = 123,
            Fields = new List<FieldEM>() {
                new TextFieldEM() { Id = 1, Caption = "Text Line", Value = "This is test" },
                new CheckBoxEM() { Id = 2, Caption = "Check here", ValueData = true }
            }
        };
        return View(em);
    }
    [HttpPost]
    public virtual ActionResult Edit(PageEM em) {
        if (!ModelState.IsValid)
            return View(em);
        // but all of the em.Fields are FieldEM.
    }
}

So how do I get it to post back with the subclassed FieldEMs?

1 Answer 1

2

You can't do that with the DefaultModelBinder. You'll have to create your own custom model binder in order to do what you want to do.

These might be helpful:

https://gist.github.com/joelpurra/2415633

ASP.NET MVC3 bind to subclass

ASP.NET MVC 3: DefaultModelBinder with inheritance/polymorphism

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

1 Comment

Thanks ataravati. I will look into this.

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.