Validating form with DataAnnotations in MVC

1

I'm trying to validate forms using DataAnnotation, I tried directly on model or my viewmodel and none of them works.

View Model:

public class CieloViewModelTransacaoPartial
{
    [Required(ErrorMessage = "Token é obrigatório")]
    public string Token { get; internal set; }

    [Required(ErrorMessage = "IDC é obrigatório")]
    [Display(Name = "IDC")]
    public int ClienteId { get; set; }

    [Required(ErrorMessage = "Valor a ser cobrado é obrigatório")]
    [Range(1, 50000)]
    public decimal Valor { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    [Required(ErrorMessage = "Data de Refêrencia é obrigatórico")]
    [Display(Name = "Data de Refêrencia")]
    [DataType(DataType.Date)]
    public DateTime DataRef { get; set; }

    public int Tipo { get; set; } //1 config, 2mensal, 3 outros, 4 diff. mensal
}

Summary View:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="panel-body">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.Token, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Token, new { htmlAttributes = new { @class = "form-control" } })

                    @Html.ValidationMessageFor(model => model.Token, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ClienteId, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-4">
                    @Html.EditorFor(model => model.ClienteId, new { htmlAttributes = new { @class = "form-control form-peq" } })

                    @Html.ValidationMessageFor(model => model.ClienteId, "", new { @class = "text-danger" })
                    <a href="#" onclick="CarregarJSON();">Carregar cliente</a>
                </div>
                <div class="col-md-6 pull-left">
                    <div id="ClienteUrl"></div>
                </div>
            </div>
        </div>
        <div class="panel-footer">
            <div class="pull-right">
                <input type="submit" value="Cadastrar" class="btn btn-danger " /></div>
        </div>
    </div>
}

My _Layout already calls the javascript below:

Butheispostingwithoutvalidating.Thereisnoerrorintheconsoleoranyerror.IwanttovalidateinthebrowserwithDataAnnotation,whatismissing?

update:_LayoutCode

<bodyclass="@ViewBag.PageClass @WMB.App_Helpers.Settings.CurrentTheme">
    @{ Html.RenderPartial("_Header", RenderSection("topright", false)); }
    @{ Html.RenderPartial("_Aside"); }
    <div id="main" role="main">
        @{ Html.Partial("_Ribbon"); }
        @RenderBody()
    </div>
    @if (Request.IsAuthenticated)
    {
        Html.RenderPartial("_Footer");
    }


    @{ Html.RenderPartial("_Scripts"); }
     @RenderSection("pagespecific", false)
</body>

and the page _Scripts.cshtml

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script> if (!window.jQuery) { document.write('<script src="/scripts/jquery-2.2.0.min.js"><\/script>'); } </script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script> if (!window.jQuery.ui) { document.write('<script src="/scripts/libs/jquery-ui-1.11.4.min.js"><\/script>'); } </script>

<!-- IMPORTANT: SmartAdmin Core -->
@Scripts.Render("~/scripts/smartadmin")

<!-- DO NOT REMOVE : GLOBAL FUNCTIONS! -->
<script>
    $(document).ready(function () {
        pageSetUp();
        drawBreadCrumb();
    });
$(document).ready(function () {
    pageSetUp();
    drawBreadCrumb();
});
function MostraConfirm(title, texto) {
    $.bigBox({
        title: title,
        content: texto,
        color: "#78C469",
        //timeout: 6000,
        icon: "fa fa-warning shake animated",
        number: "1",
        timeout: 6000
    });

}
</script>

@if (@Model is HelperResult)
{
    @Model
}
    
asked by anonymous 02.05.2016 / 16:57

1 answer

1

The error was the lack of 2 key in web.config

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

As described in detail link

    
02.05.2016 / 18:55