Generic type in action MVC

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

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

    protected Polis()
    {
    }

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

In the action of my control, the generic type must inherit this abstract class. But he does not see the constructor of the abstract class, which has an argument. Therefore, I must indicate that it implements 'new ()', instead I would like to use a 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),
        });
    }

All data processing is done inside the inhereting classes, so I need the methods of the inheriting classes that will be executed. But when I try to call a controller action that indicates my particular type as an argument, it has the error "No overload for the SavePolis method takes 0 arguments":

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

, ? , , .

+2
2

, . Polis: SubPolisA SubPolisB, 2 :

  • public ActionResult SavePolis ( SubPolisA)
  • public ActionResult SavePolis ( SubPolisA)

:

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() ,

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

ModelBinderAttribute. , .

*

+1

, , , :

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

, PolisPostModel.

0

All Articles