Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC5 - Data Annotations - Client Side Validation Not Happening?

I have an MVC 5 app, and I'm using data annotations to do a majority of validation. One of the properties in my class looks like this:

[Required(ErrorMessage = "Please enter a business name")]
[StringLength(80)]
public string BusinessName { get; set; }

The validation is working but it doesn't appear to be happening in the browser like I thought it should. On my page I have a Save button. If I leave the Business Name field blank and click Save, a post is done to a controller method that looks, partially, as follows:

    [HttpPost]
    public ActionResult Create(Advertiser advertiser, FormCollection collection, HttpPostedFileBase file)
    {
        // Before we do anything, let's check to make sure any validation that's already been done is clean.
        if (!ModelState.IsValid)
        {
            return View(advertiser);
        }
   ...
   ...
}

When this method is executed, the model state is already set to invalid. That's good because it is invalid because the Business Name field is empty. However, shouldn't this validation be happening in the client?

The field in my .cshtml file looks as follows (using Bootstrap):

<div class="form-group">
    @Html.Label("Business Name", new { @class = "control-label col-md-3" })
    <div class="col-md-9">
        @Html.TextBoxFor(model => model.BusinessName, new { @class = "form-control", title = "", autofocus = true })
        @Html.ValidationMessageFor(model => model.BusinessName)
    </div>
</div>

My Web.Config is set correctly as follows:

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
like image 694
Randy Minder Avatar asked Feb 06 '15 12:02

Randy Minder


People also ask

How do you remove data annotation validation on client side?

To disable client side validation, we need to disable it by force. Notice the @data_val= “false”. It will disable the validation on this field.

Is data annotation server side validation?

Note: By default the validation done using Data Annotation attributes is Server Side. And hence to make it work Client Side, the Client Side validation must be enabled. The following Model class consists of one property Name to which the Required Data Annotation attribute has been applied.

Can we do validation in MVC using data annotations?

In Asp.net MVC, we can easily apply validation to web application by using Data Annotation attribute classes to model class. Data Annotation attribute classes are present in System.


1 Answers

I found my problem. In my BundleConfig.cs I have the following:

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.min.js",
                "~/Scripts/jquery-ui-1.10.4.min.js",
                "~/Scripts/jquery.base64.js"
                ));

    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate.min.js",
                "~/Scripts/jquery.validate.unobtrusive.min.js"
                ));

But, what I didn't realize is that the jqueryval bundle DOES NOT get loaded in the _Layout.cshtml file by default. So I needed to add it, as follows:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

Once I did this, it is working as it should. Of course, this will cause it to get loaded for all pages. That may not be desirable. If not, load it separately in each page as necessary.

like image 154
Randy Minder Avatar answered Sep 20 '22 15:09

Randy Minder