更改视图 MVC 2 导致处理帖子时出现问题
我有一个具有以下操作的控制器:
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)
{
}
我的 create.aspx 和 Details.aspx 页面都有一个提交按钮。 create.aspx页面上的提交将导致一条记录插入数据库,然后进入详细信息视图。该部分工作正常,我可以单击提交按钮,记录将被插入并转到该记录的详细信息视图。现在,如果我在详细信息视图中单击提交,仍然会调用 Create(MyModel model)。不应该调用 Details(MyModel model) 方法吗?
在创建帖子的方法中,我想转移到详细信息视图并传递当前模型,这样就不必从数据库重新加载该数据。
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 view, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在详细信息视图中将 Html.BeginForm 更改为
当您在创建操作中返回“详细信息”视图时,框架不会猜测您的意图。
因此,它呈现“详细信息”视图,但仍然认为它是一个创建操作,并且 Html.BeginForm() 辅助方法回发到同一操作。
in your details view alter your Html.BeginForm to
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.