Asp.Net MVC Validators passing through the controller

2

When we create a project in Asp.Net it by default inserts some validators to be used with Razor, @Html.ValidationMessageFor(model => model.property) .

I saw in different projects that these messages are generated by returning the controller , that is, when the form is sent, it checks the ModelState and according to the response, it brings the required fields and so "active "the message for what is missing, however, I have also seen in some projects that it does not need to enter controller to perform the verification, what I need to know is what defines this situation.

What does the validator do before entering controller to know which fields are required to be filled in?

EDITION:

I think it's better to insert the javascripts that perform the validations and also the others I use to find out if any are getting in the way.

Follow the code below:

<script src="~/Scripts/jQuery-2.1.4.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
    <script src="~/Scripts/bootstrap.min.js" type="text/javascript"></script>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/template/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />

    @*USAR NESTA ORDEM*@
    <link href="~/Content/template/css/skins/_all-skins.min.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/jquery-confirm.css" rel="stylesheet" type="text/css" />
<link href="~/Content/meucs.css" rel="stylesheet" type="text/css" />

<script src="~/Scripts/angular.min.js"></script>
<script>angular.module("Angular", []);</script>
<script src="~/Scripts/tecbox/MainController.js"></script>    
<script src="~/Scripts/modernizr-2.8.3.js" type="text/javascript"></script>
    
asked by anonymous 25.08.2015 / 19:46

2 answers

2

I created a form with the following in Razor:

<div class="form-group">
    @Html.LabelFor(model => model.DataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.DataNascimento, "{0:dd/MM/yyyy}", new { @class = "form-control datepicker", size = "16" })
        @Html.ValidationMessageFor(model => model.DataNascimento, "", new { @class = "text-danger" })
    </div>
</div>

This generates the following HTML:

<div class="form-group">
    <label class="control-label col-md-2" for="DataNascimento">Data de Nascimento</label>
    <div class="col-md-10">
        <input class="form-control datepicker hasDatepicker" data-val="true" data-val-date="O campo Data de Nascimento deve ser uma data." data-val-required="O campo Data de Nascimento é obrigatório." id="DataNascimento" name="DataNascimento" size="16" type="text" value="" aria-required="true">
        <span class="field-validation-valid text-danger" data-valmsg-for="DataNascimento" data-valmsg-replace="true"></span>
    </div>
</div>

Note that you have several annotations beginning with "data-" and "aria-". The WAI-ARIA specification is here . About data- has something here . jQuery Validation uses these attributes to know what to validate . This is why the request does not even go to the Controller : JS performs some validation and only then does it allow the form to be finally sent.

I tried not to pass a date on the form. Here's what happened:

<div class="form-group">
    <label class="control-label col-md-2" for="DataNascimento">Data de Nascimento</label>
    <div class="col-md-10">
        <input class="form-control datepicker hasDatepicker input-validation-error" data-val="true" data-val-date="O campo Data de Nascimento deve ser uma data." data-val-required="O campo Data de Nascimento é obrigatório." id="DataNascimento" name="DataNascimento" size="16" type="text" value="" aria-required="true" aria-describedby="DataNascimento-error">
        <span class="text-danger field-validation-error" data-valmsg-for="DataNascimento" data-valmsg-replace="true"><span id="DataNascimento-error" class="">O campo Data de Nascimento é obrigatório.</span></span>
    </div>
</div>

Several things were changed by themselves.

Typically, to use with ASP.NET MVC, the following inclusion already makes everything ready for you.

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

The Bundle looks like this:

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
        "~/Scripts/jquery.validate*"));
    
25.08.2015 / 21:09
1

Validations that are performed on the client with ASP.Net MVC use JQuery Validation ( link ).

p>

In this case, simpler validations such as required for example can be made in the browser before they are performed by the Controller.

    
25.08.2015 / 20:28