我的注册视图上的所有字段都收到相同的模型错误消息。这是我的 Register Post 方法的代码。
[CaptchaMvc.Attributes.CaptchaVerify("Captcha is not valid")]
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterRequest model)
{
if (ModelState.IsValid)
{
if (Membership_BAL.UsernameExist(model.UserName.Username))
{
ModelState.AddModelError("CustomError", "Usename is taken");
return View();
}
if (Membership_BAL.EmailExist(model.EmailAddress.EmailAddress))
{
ModelState.AddModelError("CustomError", "Email Address is taken");
return View();
}
if (
!Membership_BAL.CamparaEmailAddress(model.EmailAddress.EmailAddress,
model.EmailAddress.ComfirmEmailAddress))
{
ModelState.AddModelError("CustomError", "Email Address must match");
return View();
}
Membership_BAL.Register(model);
// TODO: Redirect use to profile page
return RedirectToAction("Index", "Home");
}
TempData["Message"] = "Error: captcha is not valid.";
return View();
}
这是视图
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<fieldset>
<legend> Register Form</legend>
<ol>
<li>
@Html.LabelFor(m => Model.UserName.Username)
@Html.EditorFor(m => Model.UserName.Username)
@Html.ValidationSummary()
</li>
<li>
@Html.LabelFor(m => Model.FirstName)
@Html.EditorFor(m => Model.FirstName)
</li>
<li>
@Html.LabelFor(m => Model.LastName)
@Html.EditorFor(m => Model.LastName)
</li>
<li>
@Html.LabelFor(m => Model.EmailAddress.EmailAddress)
@Html.EditorFor(m => Model.EmailAddress.EmailAddress)
@Html.ValidationSummary()
</li>
<li>
@Html.LabelFor(m => Model.EmailAddress.ComfirmEmailAddress)
@Html.EditorFor(m => Model.EmailAddress.ComfirmEmailAddress)
@Html.ValidationSummary()
</li>
<li>
@Html.MathCaptcha()
@TempData["Message"]
</li>
</ol>
<input type="submit" value="Register">
</fieldset>
}