【发布时间】:2017-12-10 02:33:28
【问题描述】:
一段时间以来,我一直在尝试修复表单上的验证,但无法使其正常工作。验证工作正常,除非我输入有效的电子邮件地址。如果我这样做了,那么它会出于某种原因跳过所有其他验证。
另外,如果我在拆分控制器中的所有内容时做得正确,我将不胜感激。是否应该有 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中不检查Model是否有效。
标签: c# asp.net-mvc unobtrusive-validation