【问题标题】:MVC RememberMe Validation GlitchMVC RememberMe 验证故障
【发布时间】:2013-08-07 09:20:12
【问题描述】:

在我的 MVC 项目中,验证器无意中要求在登录时选中“记住我”复选框。在 IE8 上,除非用户勾选“记住我”框,否则它将不允许用户登录。所有其他浏览器都很好,包括 IE7。

<input id="RememberMe" type="checkbox" value="true" name="RememberMe" data-val-required="The Remember me? field is required." data-val="false"> 

我试图将实体框架中的属性更改为可为空的布尔值?但这会产生错误:

The best overloaded method match for 'WebMatrix.WebData.WebSecurity.Login(string, string, bool)' has some invalid argument 

所以,看来我无法更改它来解决此问题。我也尝试过用 jQuery 编写属性,但它被改回来了。这真的是随机的,我很惊讶这看起来是开箱即用的技术。任何帮助将不胜感激。

这是登录模型:

public class LoginModel
{
    [EmailAddressAttribute]
    [Required]
    [StringLength(80)]
    [Display(Name = "Email Address")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

这是进行登录的控制器,您可以看到它是开箱即用的 MVC 东西:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
    {
        return RedirectToLocal(returnUrl);
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}

这是视图:

@Html.ValidationSummary(true)

<ol class="form">
    <li>
        @Html.LabelFor(m => m.UserName)
        @Html.TextBoxFor(m => m.UserName, new { id="txUserName" })
        @Html.ValidationMessageFor(m => m.UserName)
    </li>
    <li>
        @Html.LabelFor(m => m.Password)
        @Html.PasswordFor(m => m.Password)
        @Html.ValidationMessageFor(m => m.Password)
    </li>
    <li>
        @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
        @Html.CheckBoxFor(m => m.RememberMe)
    </li>
    <li class="form-buttons">
        <input type="submit" value="Log in" /> &nbsp; @Html.ActionLink("Forgot Password", "ForgotPassword", "Account")
    </li>
</ol>

【问题讨论】:

  • 贴出处理登录的代码
  • 好的,我发布了代码,但不小心覆盖了旧的描述:(
  • 您对此有意见吗?
  • 我刚刚添加了视图。谢谢。
  • 定义“不允许用户登录”。你有错误吗?登录失败吗?您是否收到无效的模型验证消息?

标签: asp.net-mvc entity-framework validation


【解决方案1】:

经过一番研究,据说这只是Internet Explorer 10中的IE7 & IE8兼容模式造成的。

在相同条件下运行不显眼的 jquery 验证也会导致其他问题。

【讨论】:

  • 不要认为你可以。只需在模拟 IE 中进行测试,而不是在兼容模式下进行测试。
猜你喜欢
  • 2020-03-01
  • 2015-02-10
  • 2019-02-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多