MVC3剃须刀editortemplate抽象类 [英] mvc3 razor editortemplate with abstract classes

查看:175
本文介绍了MVC3剃须刀editortemplate抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从<一个跟进的问题href=\"http://stackoverflow.com/questions/10550486/mvc3-razor-httppost-return-complex-objects-child-collections\">MVC3剃刀httppost返回复杂的对象子集合的。

我给这个例子很简单。子集合实际上是都来自一个抽象基类对象的集合。因此,收藏有基类的列表。

The example I gave was very simple. The child collection is actually a collection of objects that all come from an abstract base class. So the collection has a list of base classes.

我已经创建了每个派生类模板,使用,如果孩子是类型然后给模板名称作为字符串尝试。这些模板呈现给看法,但没有填充在回发。

I have created a template for each derived class and tried using if child is of type then give the template name as a string. The templates are rendered to the view but not populated on the post back.

我不知道我是如何使用editorfor位的模板来选择正确的模板,并得到其父容器内将被编组回子对象的信息。

I am not sure how I use the editorfor bit with the templates to choose the correct template and get the information to be marshalled back into the child objects within the parent container.

推荐答案

您可以使用自定义模型粘合剂。让我们举一个例子。

You could use a custom model binder. Let's take an example.

型号:

public class MyViewModel
{
    public IList<BaseClass> Children { get; set; }
}

public abstract class BaseClass
{
    public int Id { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ModelType
    {
        get { return GetType().FullName; }
    }
}

public class Derived1 : BaseClass
{
    public string Derived1Property { get; set; }
}

public class Derived2 : BaseClass
{
    public string Derived2Property { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Children = new BaseClass[]
            {
                new Derived1 { Id = 1, Derived1Property = "prop1" },
                new Derived2 { Id = 2, Derived2Property = "prop2" },
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // everything will be fine and dandy here
        ...
    }
}

查看(〜/查看/主页/ Index.cshtml

@model MyViewModel

@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Children.Count; i++)
    {
        @Html.EditorFor(x => x.Children[i].ModelType)
        <div>
            @Html.EditorFor(x => x.Children[i].Id)
            @Html.EditorFor(x => x.Children[i])    
        </div>
    }

    <button type="submit">OK</button>
}

Dervied1 编辑器类型的模板(〜/查看/主页/ EditorTemplates / Derived1.cshtml

@model Derived1
@Html.EditorFor(x => x.Derived1Property)

和对 Dervied2 键入编辑模板(〜/查看/主页/ EditorTemplates / Derived2.cshtml

and the editor template for the Dervied2 type (~/Views/Home/EditorTemplates/Derived2.cshtml):

@model Derived2
@Html.EditorFor(x => x.Derived2Property)

现在,所有剩下的是将使用隐藏字段的值来实例化集合中的正确类型的定制模型绑定:

Now all that's left is a custom model binder that will use the hidden field value to instantiate the proper type in the collection:

public class BaseClassModelBinder : 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(BaseClass), new BaseClassModelBinder());

这篇关于MVC3剃须刀editortemplate抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆