我在编辑我的应用程序 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");
}