在后法和ViewBag燮pression MVC3 RedirectToAction [英] MVC3 RedirectToAction in a post method and ViewBag suppression

查看:117
本文介绍了在后法和ViewBag燮pression MVC3 RedirectToAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作数据的列表,我需要在重新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 跨一个请求,坚持信息和它意味着这个确切的用例。而不是使用 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();
}

请参阅和<一间href=\"http://stackoverflow.com/questions/473520\"> ASP.NET的TempData仍然存在href=\"http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications\">When使用ViewBag,ViewData的,或TempData的在ASP.Net MVC 3 。

这篇关于在后法和ViewBag燮pression MVC3 RedirectToAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆