开发者

Using two different Models in controller action for POST and GET

I h开发者_如何学运维ave "Add" method in one of my controllers in MVC project. On a normal "GET" I want to return Strongly-Typed object of type CaseEditModel and on POST verb I want the view to return an object of type Case to the controller. Is that possible?


Yes, on a get your Add action can return CaseEditModel to the view and on a post the argument for the Add action can be of type Case. On the post the model binder will try and bind to whatever you put in for an argument.

[HttpGet]
public ActionResult Add()
{
    var caseEdit = new CaseEditModel();
    return View(caseEditModel);
}

[HttpPost]
public ActionResult Add(Case caseIn)
{

}


The Request object has the requesttype property to do just that:

if (Request.RequestType == "GET")
{
     // do CaseEditModel here
}
else if (Request.RequestType == "POST")
{
     // do Case here
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜