为什么我的验证不起作用? asp.net MVC
why my validation is not working? asp.net mvc
我试图在我的视图中放置一个 ValidationMessageFor
来控制数据,但它不起作用,数据转到我的 post 操作。
这是我的模型class:
public enum CustomerType
{
JuridicalPerson,
NaturalPerson
}
public class Customer
{
[Key]
[DisplayName("ID")]
public int CustomerId { get; set; }
[Required(ErrorMessage = "Please enter your first name")]
[DisplayName("First name")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[Required(ErrorMessage ="Please enter your last name")]
[DisplayName("Last name")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[Required(ErrorMessage ="Please choose your customer type")]
[DisplayName("Customer type")]
public CustomerType CustomerType { get; set; }
这是我的创建客户视图:
@model Contracts.Models.Customer
@using (Html.BeginForm())
{
<div class="box">
<div class="input-container">
@Html.LabelFor(model => model.FirstName)
<br />
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="input-container">
@Html.LabelFor(model => model.LastName)
<br />
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="input-container">
@Html.LabelFor(model => model.CustomerType)
<br />
@Html.EnumDropDownListFor(model => model.CustomerType, "Select a type", new {@class ="input-container" })
@Html.ValidationMessageFor(model => model.CustomerType)
</div>
<input class="btn" type="submit" value="Create" />
</div>
}
最后这是我的客户控制器及其创建客户的操作:
[HttpPost]
public ActionResult Create(Customer customer)
{
//db is my database context
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
实际上在我看来 html 辅助验证,空输入应该给出错误并且不会执行我的操作但它不起作用
如果你能帮助我,我将不胜感激
尝试使用
if (ModelState.IsValid){
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");} return Page();
我试图在我的视图中放置一个 ValidationMessageFor
来控制数据,但它不起作用,数据转到我的 post 操作。
这是我的模型class:
public enum CustomerType
{
JuridicalPerson,
NaturalPerson
}
public class Customer
{
[Key]
[DisplayName("ID")]
public int CustomerId { get; set; }
[Required(ErrorMessage = "Please enter your first name")]
[DisplayName("First name")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[Required(ErrorMessage ="Please enter your last name")]
[DisplayName("Last name")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[Required(ErrorMessage ="Please choose your customer type")]
[DisplayName("Customer type")]
public CustomerType CustomerType { get; set; }
这是我的创建客户视图:
@model Contracts.Models.Customer
@using (Html.BeginForm())
{
<div class="box">
<div class="input-container">
@Html.LabelFor(model => model.FirstName)
<br />
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="input-container">
@Html.LabelFor(model => model.LastName)
<br />
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="input-container">
@Html.LabelFor(model => model.CustomerType)
<br />
@Html.EnumDropDownListFor(model => model.CustomerType, "Select a type", new {@class ="input-container" })
@Html.ValidationMessageFor(model => model.CustomerType)
</div>
<input class="btn" type="submit" value="Create" />
</div>
}
最后这是我的客户控制器及其创建客户的操作:
[HttpPost]
public ActionResult Create(Customer customer)
{
//db is my database context
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
实际上在我看来 html 辅助验证,空输入应该给出错误并且不会执行我的操作但它不起作用
如果你能帮助我,我将不胜感激
尝试使用
if (ModelState.IsValid){
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");} return Page();