【问题标题】:Pass ModelState to a RedirectToAction将 ModelState 传递给 RedirectToAction
【发布时间】:2014-04-05 19:09:37
【问题描述】:

有没有办法将 ModelState 传递给 RedirectToAction不使用 TempData

更新

我有以下操作:

public ActionResult Index()
{
    var model = SettingsService.GetSettings();
    return View(model);
}

[HttpPost]
public ActionResult Update(Settings model)
{
     if (ModelState.IsValid)
     {
         SettingsService.UpdateSettings(model);
     }

     return RedirectToAction("Index");
}

模型存在服务器验证错误。我想在重定向到索引后显示错误。

应用程序在云端,它至少有两个 WebRoles,每台机器都有自己的会话,因此我不能使用 TempData(它使用会话)。

【问题讨论】:

  • 不,根据我的观点你需要TEMPDATA
  • 你能举例说明你为什么需要这样做吗?
  • 当然,我添加了一些插图

标签: asp.net-mvc azure modelstate asp.net-mvc-validation


【解决方案1】:

如果您无法使用 Session,则可能已修复。通过一些配置,您可以使用 Windows Azure 的会话状态提供程序。就共享存储而言,它是无状态的,您的 WebRoles 也保持无状态。

这里是您可以获得所有说明的地方:http://msdn.microsoft.com/en-us/library/windowsazure/gg185668.aspx

【讨论】:

    【解决方案2】:

    您可以尝试将ModelState 作为参数传递,或者至少复制它或您需要的属性:

    RedirectToAction("MyAction", 
       new System.Web.Routing.RouteValueDictionary() { { "modelState", ModelState } });
    

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 2010-09-21
      相关资源
      最近更新 更多