Binding Model Classes and Derived Models

Does ASP.NET MVC offer an easy way to get model binding to work if you have model classes that inherit from others?

In my script, I have a view that is strongly typed for List<Person>.

I have several classes that inherit from Person, namely PersonTypeOneand PersonTypeTwo.

I have three strongly typed partial representations with names that correspond to these class names (and display form elements for the properties of their respective models).

This means that in my main view, I might have the following code:

<% for(int i = 0; i < Model.Count; i++)
   { 
       Html.RenderPartial(Model[i].GetType().Name, Model[i]);
   } %>

This works well, in addition, when the user submits the form, the corresponding controller action method simply receives List<Person>, not a list Person, PersonTypeOneand PersonTypeTwo.

This is almost as expected, as the form view does not contain enough information to tell the default binder to create any instances of the classes PersonTypeOneand PersonTypeTwo.

So, is there a way to get this functionality from a standard binder?

+3
source share
1 answer

. ViewModels EditModels - , , .

+4

Source: https://habr.com/ru/post/1748010/


All Articles