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
}
精彩评论