1

我有一个注册表格和一个用户登录表格。我显然想在注册表单上进行验证,但我不想在登录表上进行验证。我注意到它在登录表单上使用 JS 验证内容。IE 它甚至不会尝试提交表单,直到用户输入长度超过 5 的密码(即我在验证中设置的密码)。如何禁用此功能?

登录表单:

@using (Html.BeginForm("Login", "Users", FormMethod.Post, new { @class = "well"})) {

        <legend>Login</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Email, new { @placeholder = "Email" })
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Password, new { @placeholder = "Password" })
        </div>

        <label class="checkbox">
            @Html.CheckBoxFor(model => model.HasRememberMeOn) Remember me
        </label>


        if (ViewData["ErrorMessage"] != null)
        {
                <p>@ViewData["ErrorMessage"]</p>
        }
        <p>
            <input type="submit" value="Login" />
        </p>

}

模型:

[Required]
        [StringLength(20, MinimumLength=5)]
        [Username(ErrorMessage="Username must: Contain at least one letter, no spaces, no special characters")]
        public string Username { get; set; }

        [NotMapped]
        [StringLength(25, MinimumLength = 5)]
        public string Password { get; set; }

        [Required(ErrorMessage="Password is required")]
        public string HashedPassword { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 3)]
        public string Email { get; set; }

        [NotMapped]
        [Compare("Email", ErrorMessage = "Your emails don't match")]
        [DisplayName("Confirm email")]
        public string Email2 { get; set; }

        public Boolean IsActivated { get; set; }

        [DisplayName("Date joined")]
        public DateTime DateCreated { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 2)]
        [DisplayName("Real name")]
        public string Name { get; set; }
4

1 回答 1

4

正确的解决方案是在没有必需属性等的情况下为登录表单使用另一个模型。并且您只需要用户名和密码即可登录,因此无论如何使用注册模型都不是一个好主意。

于 2012-08-18T05:11:52.887 回答