泛型类型的MVC控制器动作 [英] Generic type in MVC controller action

查看:222
本文介绍了泛型类型的MVC控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器动作应该是一组继承抽象类城邦模型可用:

My controller action should be usable for a set of models that inherit the abstract class Polis:

public abstract class Polis
{
    /// <summary>
    /// Fields
    /// </summary>

    protected Polis()
    {
    }

    public Polis(Object input)
    {
        // Use input
    }
}

我CONTROLER行动指定泛型类型应该继承这个抽象类。不过,这并不看到,有一个参数抽象类的构造函数。所以,我必须指定它实现了新的(),而不是我想用的构造函数的参数。

My controler action specifies the generic type should inherit this abstract class. But it does not see the constructor of the abstract class that has an argument. So I have to specify that it implements 'new()', instead I would like to use the constructor with an argument.

    public virtual ActionResult SavePolis<TModel>(PolisPostModel polisPM) where TModel : Polis, new()
    {
        if (ModelState.IsValid)
        {
            // Get the object or save a new object in the database
        }

        return Json(new
        {
            success = ModelState.IsValid,
            status = this.GetStatus(polisPM),
        });
    }



所有的数据处理是inhereting类中完成的,所以我需要的方法要执行的继承的类。
但当我尝试调用控制器动作给我的具体类型作为参数有错误无过载方法'SavePolis'取0论据:

All data handling is done inside the inhereting classes, so I need the methods of the inheriting classes to be executed. But when I try to call the controller action giving my specific type as argument it has the error "No overload for method 'SavePolis' takes 0 arguments":

@Html.Hidden("SaveMyPolis", Url.Action(MVC.Controller.SavePolis<MyPolis>())

那么,什么是调用这个正确的方法呢,有没有可能继承的类是由完全可用,所以它的方法被调用,而不是那些从抽象类。

So what is the correct way to call this? And is it possible that the inherited class is made fully available, so it's methods are called instead of those from the abstract class.

推荐答案

我会用一个自定义的模型绑定器,这和多种操作方式接受子假设城邦的两个子类:SubPolisA和SubPolisB,我有2个动作方法:

I would use a custom modelbinder for this and multiple action methods accepting the subclasses. Assuming two subclasses of Polis: SubPolisA and SubPolisB, I would have 2 action methods:


  1. 公众的ActionResult SavePolis(SubPolisA模型)

  2. 公众的ActionResult SavePolis (SubPolisA模型)

然后我会定义城邦自定义ModelBinder的:

then I would define a custom modelbinder for polis:

public class PolisModelBinder : System.Web.Mvc.IModelBinder
{
   public object BindModel(ControllerContext controllerContext, 
                            ModelBindingContext bindingContext)
    {
         var form = controllerContext.HttpContext.Request.Form;
         //use hidden value to determine the model
         if(form.Get("PolisType") == "SubClassA") 
         {
            //bind SubPolisA
         }
         else 
         {
            //bind SubPolisB
         }
    }
}

那么的Application_Start()我会使用注册模型绑定

then in Application_Start() I would register the model binder using

ModelBinders.Binders.Add(typeof(SubPolisA), new PolisModelBinder());
ModelBinders.Binders.Add(typeof(SubPolisB), new PolisModelBinder());

或使用ModelBinderAttribute。如果你愿意,你也可以使用多种模型粘合剂每个子类。

or use the ModelBinderAttribute. If you want to you could also use multiple model binders for each subclass.

*对不起代码格式化

这篇关于泛型类型的MVC控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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