编辑我的应用程序RegisterModel时遇到两个问题。
A)UserName和Email字段是否显示为密码字段?
B)模型状态始终无效(我的模型为空)
我认为它们都是因为我有一个“HomeModel”,它包含“LoginModel”和“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字段呈现为密码字段。 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");
}
答案 0 :(得分:1)
问题A:您使用@ Html.PasswordFor()呈现电子邮件和用户名的文本字段,这将呈现密码字段,尝试使用@ Html.TextboxFor()
对于问题B,它取决于您是否针对MVC3或4以及哪个版本的.NET。 更高版本的.NET使用比较注释作为
[Compare(CompareField = Password, ErrorMessage = "Passwords do not
match")]
答案 1 :(得分:0)
a)因为你有剃刀视图
@Html.PasswordFor(m => m.RegisterModel.UserName)
需要
@Html.TextboxFor(m => m.RegisterModel.Email)