[英]Rerouting to different method in same controller with POST
I have 2 index methods in a controller, one get to load the page initially and one post to accept form submit and reload the same view based on the submitted data. 我在控制器中有2种索引方法,一种可以首先加载页面,另一种可以接受表单提交并根据提交的数据重新加载相同的视图。
[HttpGet]
public ActionResult Index()
{
loads page with viewmodel containing a searchobject that is submitted with a form on the page
}
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Index(SearchModel Model)
{
takes form submit and reloads page based on search results
return View(Model);
}
I also have an Add view/method in the same controller. 我在同一控制器中也有一个添加视图/方法。 What I'm wanting to do is when the add function is successful, I'd like to call the POST version of the index and pass it the id of the item that was just added.
我想要做的是add函数成功时,我想调用索引的POST版本,并将刚刚添加的项目的ID传递给它。
here's what I have so far: 这是我到目前为止的内容:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Add(AddModel addmodel)
{
do stuff
var status = client.Add(dto);
if (!status)
{
return View(AddModel);
}
SearchModel = new SearchModel();
SearchModel.ID= addmodel.ID;
return Index(search, null);
}
It enters the POST index method but after it leaves that method it goes back to the add and tries to load the add page with the search viewmodel and obviously throws an exception. 它进入POST索引方法,但是在离开该方法之后,它返回到add并尝试使用搜索viewmodel加载add页面,并且显然引发异常。
Any ideas what my options are to do something like this? 有什么想法我可以做这样的事情吗?
By default View(Model)
uses name of current action (the one routed to - Add
in your case, not the current method name - Index
). 默认情况下,
View(Model)
采用电流动作(一排到-的名字Add
在你的情况,而不是当前方法的名称- Index
)。
Fix: specify view name explicitly: 修复:明确指定视图名称:
public ActionResult Index(SearchModel Model)
{
takes form submit and reloads page based on search results
return View("Index", Model);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.