我一直在嘗試修復窗體上的驗證一段時間,但無法使其正常工作。驗證工作正常,除非我輸入有效的電子郵件地址。如果我這樣做,那麼由於某種原因,它會跳過所有其他驗證。無法修復ASP.NET MVC驗證
此外,將不勝感激關於如果我正在與控制器中的所有東西的分裂正確的做法。如果有2個動作(1個用於GET,只需加載空表單,1個用於POST時用戶提交)?我在那裏做錯了什麼?
編輯:更正:如果我輸入一個有效的電子郵件地址,表單提交罰款,並忽略其他驗證。如果我沒有有效的電子郵件地址,驗證會檢查一切。
這裏是我的模型類:
public class User
{
public int ID { get; set; }
[DisplayName("Name")]
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[DisplayName("Email")]
[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Email is required")]
[RegularExpression(
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
ErrorMessage = "Invalid email")]
public string Email { get; set; }
[DisplayName("Password")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password required")]
[MinLength(6, ErrorMessage = "Min 6 characters")]
public string Password { get; set; }
}
這裏是我的控制器:
public ActionResult Register()
{
ViewBag.LoggedIn = false;
return View();
}
[HttpPost]
public ActionResult Register(User user)
{
ViewBag.LoggedIn = false;
user.Password = PasswordHash.CreateHash(user.Password);
using (var context = new FoundationContext())
{
// if user exists
if (context.Users.FirstOrDefault(n => n.Email == user.Email) != null) return Login(true);
context.Users.Add(user);
context.SaveChanges();
}
return View("~/Views/Home.Index.cshtml");
}
這裏是我的表格:
身體@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-10 text-center">
<input type="submit" value="Register" class="btn btn-primary" />
</div>
</div>
</div>
}
當然底部包括:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
你不檢查,看看是否模型在HttpPost行動是有效的'ModelState.IsValid '。 – Scrobi