ASP.NET MVC - 如何preserve的ModelState错误跨越RedirectToAction? [英] ASP.NET MVC - How to Preserve ModelState Errors Across RedirectToAction?

查看:149
本文介绍了ASP.NET MVC - 如何preserve的ModelState错误跨越RedirectToAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个动作方法(简化的问题):

I have the following two action methods (simplified for question):

[HttpGet]
public ActionResult Create(string uniqueUri)
{
   // get some stuff based on uniqueuri, set in ViewData.  
   return View();
}

[HttpPost]
public ActionResult Create(Review review)
{
   // validate review
   if (validatedOk)
   {
      return RedirectToAction("Details", new { postId = review.PostId});
   }  
   else
   {
      ModelState.AddModelError("ReviewErrors", "some error occured");
      return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
   }   
}

所以,如果验证通过,我重定向到另一个页面(确认)。

So, if the validation passes, i redirect to another page (confirmation).

如果发生错误,我需要与错误,以显示相同的页面。

If an error occurs, i need to display the same page with the error.

如果我这样做返回查看(),会显示错误,但如果我这样做返回RedirectToAction (如以上),就失去了模型误差。

If i do return View(), the error is displayed, but if i do return RedirectToAction (as above), it loses the Model errors.

我没有这个问题感到惊讶,只是想知道你们怎么处理?

I'm not surprised by the issue, just wondering how you guys handle this?

我当然可以只返回了同样的看法,而不是重定向,但我有逻辑在其中填充视图的数据,我不得不重复创建的方法。

I could of course just return the same View instead of the redirect, but i have logic in the "Create" method which populates the view data, which i'd have to duplicate.

有什么建议?

推荐答案

您需要有审核 HTTPGET 的行动。
要做到这一点,你应该保存对象回顾回顾的临时变量,在你的 HttpPost 操作,然后它<$恢复C $ C> HTTPGET
操作。

You need to have the same instance of Review on your HttpGet action. To do that you should save an object Review review in temp variable on your HttpPost action and then restore it on HttpGet action.

[HttpGet]
public ActionResult Create(string uniqueUri)
{
   //Restore
   Review review = TempData["Review"] as Review;            

   // get some stuff based on uniqueuri, set in ViewData.  
   return View(review);
}
[HttpPost]
public ActionResult Create(Review review)
{
   //Save you object
   TempData["Review"] = review;

   // validate review
   if (validatedOk)
   {
      return RedirectToAction("Details", new { postId = review.PostId});
   }  
   else
   {
      ModelState.AddModelError("ReviewErrors", "some error occured");
      return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
   }   
}

此外,我会建议,如果你想它也工作,当浏览器刷新按钮$ P $后 HTTPGET pssed动作执行的第一次,你可能会像

Also i would advice, if you want to make it work also when browser refresh button pressed after HttpGet action executed first time, you may go like that

  Review review = TempData["Review"] as Review;  
  TempData["Review"] = review;

否则就刷新按钮对象审核将是空的,因为不会有在 TempData的任何数据[评论]

Otherwise on refresh button object review will be empty because there wouldn't be any data in TempData["Review"].

这篇关于ASP.NET MVC - 如何preserve的ModelState错误跨越RedirectToAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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