0
votes

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")
}
3
Is client side validation enabled? Have you also included jquery{version}.js? - user3559349
Can you please verify if the controls are empty when you make fresh request to index()? Also if the client side validation libraries are included on page!! - Rohit416
Ran your code, works fine for me. I guess check Andrei Mikhalevich answer - Vivek Ranjan
Could you please post your comment as the answer @StephenMuecke.I had to add @Scripts.Render("~/bundles/jquery") and now it works - Denys Wessels

3 Answers

2
votes

These settings should be enabled. Please check your web.config:

<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

You can also try this:

@{ Html.EnableClientValidation(); }

And the last thing: make sure you don't have any client-side errors in browser's console.

1
votes

Ensure you have included jquery{version}.js in you view. For example by using @Scripts.Render("~/bundles/jquery") if your using the default bundles.

Side note, your property error messages will not display in the @Html.ValidationSummary(true) element (only in the @Html.ValidationMessageFor() element associated with each property) because you specify true for the excludePropertyErrors property.

0
votes

Check your web.config

 <add key="ClientValidationEnabled" value="true" />
 <add key="UnobtrusiveJavaScriptEnabled" value="true" />

enter image description here