【问题标题】:Validation Message not showing after Redirect (Using ModelStateToTempData attribute)重定向后未显示验证消息(使用 ModelStateToTempData 属性)
【发布时间】:2009-07-14 20:45:00
【问题描述】:

即使我使用 MVCConrib 的 ModelStateToTempData,我也遇到了重定向后不显示验证消息的问题。我是否忽略了一些基本的东西?

[ModelStateToTempData]
public class AccountController : BaseController
{
    public ActionResult LogOn(string email, string password, string returnUrl)
    {
        if (!ValidateLogOn(email, password))
        {
            return RedirectToAction("Index", "AnotherController");
        }

        //other stuff
    }

private bool ValidateLogOn(string email, string password)
{
    if (!_userTask.ValidateUser(email, password))
    {
        ModelState.AddModelError("message", "The email or password provided is incorrect.");
    }

    return ModelState.IsValid;
}
}

查看:

   <li>
        <label for="email">E-mail</label>
        <%= Html.TextBox("email")%>
        <%= Html.ValidationMessage("message") %>
    </li>  

【问题讨论】:

    标签: asp.net-mvc mvccontrib


    【解决方案1】:

    您是否使用 [ModelStateToTempData] 装饰两个控制器?

    詹姆斯

    【讨论】:

      【解决方案2】:

      您是否肯定在视图中显示验证消息?

      【讨论】:

        【解决方案3】:

        如果您在 ModelState 中有任何错误,则不应重定向到任何其他控制器。如果 ModelState 中有错误,则无法导航到其他控制器 - 最好在成功时重定向,而不是在失败时重定向。

        只需检查 ModelState 是否包含任何错误并返回您收到请求的视图。

         if (!ValidateLogOn(email, password))
         {
             return View("Index");
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-08
          • 2016-03-29
          相关资源
          最近更新 更多