视图模型与列表<&的BaseClass GT;和编辑模板

视图模型与列表<&的BaseClass GT;和编辑模板

问题描述:

我有列出被添加到一个平面图表的视图。表从 TableInputModel 导出,以便 RectangleTableInputModel CircleTableInputModel ,等等

I have a view that lists tables being added to a floor plan. Tables derive from TableInputModel to allow for RectangleTableInputModel, CircleTableInputModel, etc

该视图模型有一个List TableInputModel 这都是一个派生类型。

The ViewModel has a list of TableInputModel which are all one of the derived types.

我对每一个派生类型的局部视图,并给予了列表混合派生类型的框架,知道如何使它们。

I have a partial view for each of the derived types and given a List of mixed derived types the framework knows how to render them.

然而,在提交类型信息被丢失的形式。我曾尝试与自定义模型粘合剂但是由于类型信息会丢失,当它被提交,它不会工作...

However, on submitting the form the type information is lost. I have tried with a custom model binder but because the type info is lost when it's being submitted, it wont work...

有没有人试过这种?

假设你有以下型号:

public abstract class TableInputModel 
{ 

}

public class RectangleTableInputModel : TableInputModel 
{
    public string Foo { get; set; }
}

public class CircleTableInputModel : TableInputModel 
{
    public string Bar { get; set; }
}

和以下的控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new TableInputModel[]
        {
            new RectangleTableInputModel(),
            new CircleTableInputModel()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(TableInputModel[] model)
    {
        return View(model);
    }
}

现在你可以写意见。

主视图 Index.cshtml

@model TableInputModel[]
@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <input type="submit" value="OK" />
}

和相应的编辑器模板。

〜/查看/主页/ EditorTemplates / RectangleTableInputModel.cshtml

@model RectangleTableInputModel
<h3>Rectangle</h3>
@Html.Hidden("ModelType", Model.GetType())
@Html.EditorFor(x => x.Foo)

〜/查看/主页/ EditorTemplates / CircleTableInputModel.cshtml

@model CircleTableInputModel
<h3>Circle</h3>
@Html.Hidden("ModelType", Model.GetType())
@Html.EditorFor(x => x.Bar)

和拼图的最后缺少的和平是自定义模型绑定的 TableInputModel 键入将使用张贴隐藏字段值获取的类型和实例化的正确实施:

and final missing peace of the puzzle is the custom model binder for the TableInputModel type which will use the posted hidden field value to fetch the type and instantiate the proper implementation:

public class TableInputModelBinder : DefaultModelBinder
{
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        var typeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".ModelType");
        var type = Type.GetType(
            (string)typeValue.ConvertTo(typeof(string)), 
            true
        );
        var model = Activator.CreateInstance(type);
        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
        return model;
    }
}

将在的Application_Start 注册:

ModelBinders.Binders.Add(typeof(TableInputModel), new TableInputModelBinder());

这就是pretty所有得多。现在指数POST操作里面的模式阵列将得到妥善initialzed用正确的类型。

and that's pretty much all. Now inside the Index Post action the model array will be properly initialzed with correct types.