changing view mvc 2 causes problems handing post
I have a controller with the following actions:
public ActionResult Create()
{
return View(new MyModel());
}
[HttpPost]
public ActionResult Create(MyModel model)
{
//Update database
...
//Pass the current model so we don't have to load it from the database
return View("Details", model);
}
[HttpPost]
public ActionResult Details(MyModel model)
{
}
Both my create.aspx and Details.aspx page have a submit button. The submit on the create.aspx page will cause a record to be insert into the database, and then it goes to the details view. That part works fine, I can click the submit button, the record gets inserted and goes to the details view for that record. Now if I click submit in details vie开发者_StackOverflow社区w, the Create(MyModel model) still gets called. Shouldn't the Details(MyModel model) method get called?
In the method for the create post, I want to transfer to the details view and pass the current model, so that don't have to reload that data from the database.
in your details view alter your Html.BeginForm to
<%= Html.BeginForm("Action","Contoller", new{}) %>
When you return "Details" view in Create action, Framework will not guess your intention. As a result it renders "Details" view but still thinks that it is a Create action and Html.BeginForm() helper method posts back to same action.
精彩评论