ASP.NET MVC - 如何在 RedirectToAction 中保留 ModelState 错误? [英] ASP.NET MVC - How to Preserve ModelState Errors Across RedirectToAction?

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

问题描述

我有以下两种操作方法(针对问题进行了简化):

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.

如果我执行return View(),则会显示错误,但是如果我执行return 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 操作需要具有相同的 Review 实例.为此,您应该在 HttpPost 操作的临时变量中保存一个对象 Review review,然后在 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 your 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"]});
   }   
}

如果您希望即使在第一次执行 HttpGet 操作后刷新浏览器也能正常工作,您可以这样做:

If you want this to work even if the browser is refreshed after the first execution of the HttpGet action, you could do this:

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

否则刷新按钮对象 review 将为空,因为 TempData["Review"] 中不会有任何数据.

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

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

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