MVC 3 Model Binding a Sub Type (Abstract Class or Interface)

左心房为你撑大大i 提交于 2019-11-26 17:22:45

This can be achieved through overriding CreateModel(...). I will demonstrate that with an example.

1. Lets create a model and some base and child classes.

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";
        }
    }
}

2. Now create a modelbinder and override CreateModel

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);
    }

}

3. Now in the controller create get and post action.

[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);
    }

4. Now Set MyModelBinder as Default ModelBinder in global.asax This is done to set a default model binder for all actions, for a single action we can use ModelBinder attribute in action parameters)

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        ModelBinders.Binders.DefaultBinder = new MyModelBinder();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

5. Now we can create view of type MyModel and a partial view of type MyDerievedClass

Index.cshtml

@model MvcApplication2.Models.MyModel

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>MyModel</legend>
    @Html.EditorFor(m=>m.BaseClass,"DerievedView")
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

DerievedView.cshtml

@model MvcApplication2.Models.MyDerievedClass

@Html.ValidationSummary(true)
<fieldset>
    <legend>MyDerievedClass</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.MyProperty)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MyProperty)
        @Html.ValidationMessageFor(model => model.MyProperty)
    </div>

</fieldset>

Now it will work as expected, Controller will receive an Object of type "MyDerievedClass". Validations will happen as expected.

Marcel de Castilho

I had the same problem, I ended up using MvcContrib as sugested here.

The documentation is outdated but if you look at the samples it's pretty easy.

You'll have to register your types in the Global.asax:

protected void Application_Start(object sender, EventArgs e) {
    // (...)
    DerivedTypeModelBinderCache.RegisterDerivedTypes(typeof(ProductTypeBase), new[] { typeof(Shirt), typeof(Pants) });
}

Add two lines to your partial views:

@model MvcApplication.Models.Shirt
@using MvcContrib.UI.DerivedTypeModelBinder
@Html.TypeStamp()
<div>
    @Html.LabelFor(m => m.Color)
</div>
<div>
    @Html.EditorFor(m => m.Color)
    @Html.ValidationMessageFor(m => m.Color)
</div>

Finally, in the main view (using EditorTemplates):

@model MvcApplication.Models.Product
@{
    ViewBag.Title = "Products";
}
<h2>
    @ViewBag.Title</h2>

@using (Html.BeginForm()) {
    <div>
        @Html.LabelFor(m => m.Name)
    </div>
    <div>
        @Html.EditorFor(m => m.Name)
        @Html.ValidationMessageFor(m => m.Name)
    </div>
    <div>
        @Html.EditorFor(m => m.SubProduct)
    </div>
    <p>
        <input type="submit" value="create" />
    </p>
}

well I had this same problem and I have solved in a more general way I think. In My case I'm sending object thru Json from backend to client and from client to backend:

First of all In abstract class I have field that i set in constructor:

ClassDescriptor = this.GetType().AssemblyQualifiedName;

So In Json I Have ClassDescriptor field

Next thing was to write custom binder:

public class SmartClassBinder : DefaultModelBinder
{
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {

            string field = String.Join(".", new String[]{bindingContext.ModelName ,  "ClassDescriptor"} );
                var values = (ValueProviderCollection) bindingContext.ValueProvider;
                var classDescription = (string) values.GetValue(field).ConvertTo(typeof (string));
                modelType = Type.GetType(classDescription);

            return base.CreateModel(controllerContext, bindingContext, modelType);
        }       
}

And now all I have to do is to decorate class with attribute. For example:

[ModelBinder(typeof(SmartClassBinder))] public class ConfigurationItemDescription

That's it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!