MVC视图模型继承和创建动作 [英] MVC view model inheritance and create action

查看:84
本文介绍了MVC视图模型继承和创建动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出用于处理MVC应用程序中的模型类型层次结构的最佳体系结构.

I'm trying to work out the best architecture for handling model type hierarchies within an MVC application.

给出以下假设模型-

public abstract class Person
{
    public string Name { get; set; }
}

public class Teacher : Person
{
    public string Department { get; set; }
}

public class Student : Person
{
    public int Year { get; set; }
}

我可以为每种类型创建一个控制器.人员将仅具有使用显示模板的视图的index和detail操作,而Teacher和Student将仅具有Create/Edit操作.那会行得通,但似乎很浪费,而且无法真正扩展,因为如果在层次结构中添加其他类型,则需要一个新的控制器和视图.

I could just create a controller for each type. Person would have just the index and detail action with the views making use of display templates, Teacher and Student would have just the Create/Edit actions. That would work but seems wasteful and wouldn't really scale because a new controller and views would be needed if another type was added to the hierarchy.

是否可以在Person控制器中执行更通用的Create/Edit动作?我已经搜索了一段时间了,但是似乎找不到我想要的东西,所以任何帮助或指点都将不胜感激:)

Is there a way to make a more generic Create/Edit action within the Person controller? I have searched for the answer for a while but can't seem to find exactly what I am looking for so any help or pointers would be appreciated :)

推荐答案

当然可以,但是需要花费一些时间.

Sure, but it takes a little leg work.

首先,在每个编辑/创建视图中,都需要发出要编辑的模型的类型.

First, in each of your edit/create views, you need to emit the type of model you are editing.

第二,您需要为person类添加一个新的modelbinder.这是我为什么要这样做的一个示例:

Second, you need add a new modelbinder for the person class. Here is a sample of why I would do for that:

public class PersonModelBinder :DefaultModelBinder
{

    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        PersonType personType = GetValue<PersonType>(bindingContext, "PersonType");

        Type model = Person.SelectFor(personType);

        Person instance = (Person)base.CreateModel(controllerContext, bindingContext, model);

        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance, model);

        return instance;
    }

    private T GetValue<T>(ModelBindingContext bindingContext, string key)
    {
        ValueProviderResult valueResult =bindingContext.ValueProvider.GetValue(key);

        bindingContext.ModelState.SetModelValue(key, valueResult);

        return (T)valueResult.ConvertTo(typeof(T));
    }  
}

将其注册到您的应用开始中:

Register it in your app start:

ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());

PersonType是我倾向于在每种模型中使用的一种,它是一个枚举,说明每种类型是什么,我在HiddenFor中发出它,以便它与发布数据一起返回.

The PersonType is what I tend to use in each model and is an enum that says what each type is, I emit that in a HiddenFor so that it comes back in with the post data.

SelectFor是一种返回指定枚举类型的方法

The SelectFor is a method that returns a type for the specified enum

public static Type SelectFor(PersonType type)
    {
        switch (type)
        {
            case PersonType.Student:
                return typeof(Student);
            case PersonType.Teacher:
                return typeof(Teacher);
            default:
                throw new Exception();
        }
    }

您现在可以在控制器中执行类似的操作

You can now do something like this in your controller

public ActionResult Save(Person model)
{
    // you have a teacher or student in here, save approriately
}

Ef可以通过TPT样式继承非常有效地处理此问题

Ef is able to deal with this quite effectively with TPT style inheritance

只需完成示例:

public enum PersonType
{
    Teacher,
    Student    
}

public class Person
{ 
    public PersonType PersonType {get;set;}
}

public class Teacher : Person
{
    public Teacher()
    {
        PersonType = PersonType.Teacher;
    }
}

这篇关于MVC视图模型继承和创建动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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