I have an MVC application and a simple Product model.The ProductName property is required but when I click the Create button without providing the ProductName I don't get any validation errors and the HttpPost method for Create gets called.
1.Model:
public class Product
{
public int ProductID { get; set; }
[Required(ErrorMessage="The product name cannot be blank")]
[Display(Name="Product")]
public string ProductName { get; set; }
[Required(ErrorMessage="Please provide the creation date")]
[DataType(DataType.Date)]
public DateTime CreationDate { get; set; }
}
2.Controller:
public class ProductController : Controller
{
public ActionResult Index()
{
var product1 = new Product
{
ProductID = 0,
ProductName = "Banana",
CreationDate = DateTime.Now
};
var product2 = new Product
{
ProductID = 1,
ProductName = "Apple",
CreationDate = DateTime.Now
};
var products = new List<Product>() { product1,product2};
return View(products);
}
[HttpGet]
public ViewResult Create()
{
return View();
}
[HttpPost]
[ActionName("Create")]
public ActionResult CreateProduct()
{
return null;
}
}
3.View:
@model MVCProject.Web.UI.Models.Product
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Product</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProductName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductName)
@Html.ValidationMessageFor(model => model.ProductName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CreationDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CreationDate)
@Html.ValidationMessageFor(model => model.CreationDate)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

jquery{version}.js? - user3559349