0

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

A) 字段用户名和电子邮件被呈现为密码字段?

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

我认为它们都是因为我有一个包含“LoginModel”和“RegisterModel”属性的“HomeModel”,它传递了整个 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; }    
}

但是用户名和电子邮件字段呈现为密码字段。 http://i.imgur.com/GCamint.png - 还不能分页图片,抱歉。

而且我的模型状态总是无效的。

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

2 回答 2

1

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

对于问题 B,这取决于您的目标是 MVC3 还是 4 以及 .NET 的哪个版本。.NET 的更高版本使用比较注释作为

[Compare(CompareField = Password, ErrorMessage = "Passwords do not
match")]
于 2013-06-09T12:48:55.793 回答
0

a)因为您对他们有清晰的看法

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

需要

@Html.TextboxFor(m => m.RegisterModel.Email)
于 2013-06-09T12:49:47.363 回答