Object as a parameter for an action controller?

前端 未结 3 422
走了就别回头了
走了就别回头了 2021-01-28 09:21

Is it possible for an action controller to accept a literal object. For example, I have several views in which I would like to post various models from to a single controller th

3条回答
  •  太阳男子
    2021-01-28 10:03

    Have a quick read about how model binding works... The model binder (which takes whatever is posted to your Action and turns it into the anyModel parameter uses the type of the parameter to determine what to do.

    Since the type is Object it can't do anything.

    My guess (depending on what you're trying to achieve) is that you can have several Action overloads each with a different type of Model as the parameter which then call common code.

    [HttpPost]
    public ActionResult ProcessModel(Model1 anyModel){}
    
    [HttpPost]
    public ActionResult ProcessModel(Model2 anyModel){}
    
    [HttpPost]
    public ActionResult ProcessModel(Model3 anyModel){}
    

    That said it's a bit odd to have one action which takes lots of different models. There's a good chance you're better off doing something else.

    Your question might gather a better answer if you say what you're trying to achieve

提交回复
热议问题