Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This can be achieved through overriding CreateModel(...). I will demonstrate that with an example.</p> <p><strong>1. Lets create a model and some base and child classes</strong>.</p> <pre><code>public class MyModel { public MyBaseClass BaseClass { get; set; } } public abstract class MyBaseClass { public virtual string MyName { get { return "MyBaseClass"; } } } public class MyDerievedClass : MyBaseClass { public int MyProperty { get; set; } public override string MyName { get { return "MyDerievedClass"; } } } </code></pre> <p><strong>2. Now create a modelbinder and override CreateModel</strong></p> <pre><code>public class MyModelBinder : DefaultModelBinder { protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) { /// MyBaseClass and MyDerievedClass are hardcoded. /// We can use reflection to read the assembly and get concrete types of any base type if (modelType.Equals(typeof(MyBaseClass))) { Type instantiationType = typeof(MyDerievedClass); var obj=Activator.CreateInstance(instantiationType); bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, instantiationType); bindingContext.ModelMetadata.Model = obj; return obj; } return base.CreateModel(controllerContext, bindingContext, modelType); } } </code></pre> <p><strong>3. Now in the controller create get and post action.</strong></p> <pre><code>[HttpGet] public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; MyModel model = new MyModel(); model.BaseClass = new MyDerievedClass(); return View(model); } [HttpPost] public ActionResult Index(MyModel model) { return View(model); } </code></pre> <p><strong>4. Now Set MyModelBinder as Default ModelBinder in global.asax</strong> This is done to set a default model binder for all actions, for a single action we can use ModelBinder attribute in action parameters)</p> <pre><code>protected void Application_Start() { AreaRegistration.RegisterAllAreas(); ModelBinders.Binders.DefaultBinder = new MyModelBinder(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } </code></pre> <p><strong>5. Now we can create view of type MyModel and a partial view of type MyDerievedClass</strong></p> <p><strong>Index.cshtml</strong></p> <pre><code>@model MvcApplication2.Models.MyModel @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Index&lt;/h2&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;MyModel&lt;/legend&gt; @Html.EditorFor(m=&gt;m.BaseClass,"DerievedView") &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } </code></pre> <p><strong>DerievedView.cshtml</strong></p> <pre><code>@model MvcApplication2.Models.MyDerievedClass @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;MyDerievedClass&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.MyProperty) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.MyProperty) @Html.ValidationMessageFor(model =&gt; model.MyProperty) &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p><strong>Now it will work as expected, Controller will receive an Object of type "MyDerievedClass". Validations will happen as expected.</strong></p> <p><img src="https://i.stack.imgur.com/7u727.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload