2

I have successfully mapped my polymorphic structure into the view, however, when I am trying to post user input for a particular derived object, it is not recognizing the derived type in the controller action...

Top-level view for the base type:

@model  InformationRequestViewModel

@using (Html.BeginForm("PostInformationRequest", "SomeController", FormMethod.Post))
{
    @Html.EditorForModel()
}

View for the derived type:

@model  ActiveServiceInformationRequestViewModel

<label>Start date:</label>
@Html.TextBoxFor(x => x.StartDate, new { @class = "u-full-width" })

<label>End date:</label>
@Html.TextBoxFor(x => x.EndDate, new { @class = "u-full-width" })

Post action:

[HttpPost]
public ActionResult PostInformationRequest(InformationRequestViewModel model)
{
    if(model is ActiveServiceInformationRequestViewModel)
    {
        // IT IS NOT DIPPING INTO THIS LOGIC!

        // Do something with this particular concrete type...
    }

    // More derived type checks and relevant logic here...

    return RedirectToAction("Index");
}

Base view model:

public class InformationRequestViewModel
{
    public int Id {get; set;}
}

Derived view model:

    public class ActiveServiceInformationRequestViewModel : InformationRequestViewModel
    {
          public DateTime StartDate {get; set;}
          public DateTime EndDate {get; set;}
    }
Tomuke
  • 869
  • 2
  • 8
  • 26
  • View for the derived type references StartDate twice and not end date – Ian Jul 15 '16 at 10:09
  • I guess (currently not able to test it) you have to define an overload method for this. I assume your model will be deserialized based upon the class defined in the method signature, and therefor be of type InformationRequestViewModel. – Stijn Van Antwerpen Jul 15 '16 at 10:24
  • So you think I would need to create a separate POST action for each derived type? – Tomuke Jul 15 '16 at 10:28
  • 3
    I don't think you can do this by default as the MVC model binder will just try to create a `InformationRequestViewModel` for you, there's no way for it to know that you are posting a derived model. You could possibly get away with it if you made your own model binder. – DavidG Jul 15 '16 at 10:34

0 Answers0