ASP.. NET MVC Razor模型状态在RegisterModel上无效

本文关键字:RegisterModel 无效 状态 模型 NET MVC Razor ASP | 更新日期: 2023-09-27 18:17:40

我在编辑应用程序RegisterModel时遇到了两个问题。

A)字段UserName和Email呈现为密码字段?

B)模型状态总是无效的(我的模型是空的)

我认为它们都是由于我有一个"HomeModel",其中包含"LoginModel"answers"RegisterModel"属性,它传递整个HomeModel而不是相应的属性。我怎样才能使它通过正确的考试呢?

我有下面的表格:

@using (Ajax.BeginForm("Register", "Account", new AjaxOptions { UpdateTargetId = "RegisterAjaxResponse" }))
                        {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)
                        <div class="form-row">
                            <div id="RegisterAjaxResponse"></div>
                        </div>
                        <div class="form-row">
                            @Html.LabelFor(m => m.RegisterModel.UserName)
                            @Html.PasswordFor(m => m.RegisterModel.UserName)
                            @Html.ValidationMessageFor(m => m.RegisterModel.UserName)
                        </div>
                        <div class="form-row">
                            @Html.LabelFor(m => m.RegisterModel.Password)
                            @Html.PasswordFor(m => m.RegisterModel.Password)
                            @Html.ValidationMessageFor(m => m.RegisterModel.Password)
                        </div>
                        <div class="form-row">
                            @Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
                            @Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
                            @Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword)
                        </div>
                        <div class="form-row">
                            @Html.LabelFor(m => m.RegisterModel.Email)
                            @Html.PasswordFor(m => m.RegisterModel.Email)
                            @Html.ValidationMessageFor(m => m.RegisterModel.Email)
                        </div>
                        <div class="form-row">
                            <input type="submit" value='Register' />
                        </div>
                        }

模型:

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    [DataType(DataType.Text)]
    public string UserName { get; set; }
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "EmailAddress")]
    public string Email { get; set; }    
}

但是UserName和Email字段呈现为密码字段。https://i.stack.imgur.com/6kG79.png-还不能分页图片,抱歉。

我的modelstate总是无效的。

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
    string returnValue = "";
    if (ModelState.IsValid)
    {
    //Some code that is never executed
    }
    return Content(returnValue, "text/html");
}

ASP.. NET MVC Razor模型状态在RegisterModel上无效

问题A:您正在使用@Html.PasswordFor()呈现电子邮件和用户名的文本字段,这将呈现密码字段,尝试使用@Html.TextboxFor()

对于问题B,这取决于您的目标是MVC3还是4以及哪个版本的。net。后来的。net版本使用compare注释作为

[Compare(CompareField = Password, ErrorMessage = "Passwords do not
match")]

a)因为你有一个razor视图

  @Html.PasswordFor(m => m.RegisterModel.UserName)

必须是

@Html.TextboxFor(m => m.RegisterModel.Email)