在后法和ViewBag燮pression MVC3 RedirectToAction [英] MVC3 RedirectToAction in a post method and ViewBag suppression
问题描述
我目前工作数据的列表,我需要在重新present一个列表,显示每个项目可以执行相应的动作视图来显示,比如编辑或删除它们。为版本是没有问题的concedering,这是一个特殊的图。我的问题是,当我想删除一个项目,我有两个选择中的POST方法。
i'm currently working a list of data that i need to display in a view that represent a list and show for each item the corresponding action that can be executed, like edit them or delete them. For the edition there is no problem concedering that it's a particular view. My problem is when i want to delete an item, i have two choices in the post method.
//Call directly the list
[HttpPost]
[Authorize]
public ActionResult Delete(int itemId)
{
// logic to delete an item
ViewBag.Error = ""; // The result of the execution
return List();
}
此解决方案的一点是,URL不再相同,第一个:... /名单,这是... /立即删除,我不觉得这是解决大,其他的解决办法是重定向到的动作,现在的网址是好的,但在viewBag错误信息将不再可见,难道你们有一些更好的主意。
The thing with this solution is that the url is no longer the same as the first one : .../List, it's .../Delete now, i don't find this solution great, the other solution is to redirect to the action, now the url is good, but the error message in the viewBag is no longer visible, Do you guys have some better idea.
推荐答案
您可以使用的TempData code>跨一个请求,坚持信息和它意味着这个确切的用例。而不是使用
ViewBag
使用的TempData [错误] =;
来代替。然后,在接收端你会有类似如下的片断:
You can use TempData
to persist information across one request and it was meant for this exact use case. Rather than use the ViewBag
use TempData["Error"] = "";
instead. Then, on the receiving end you would have a snippet like the following:
[HttpGet]
public ActionResult List() {
ViewBag.Error = TempData["Error"];
// ...
return View();
}
这篇关于在后法和ViewBag燮pression MVC3 RedirectToAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!