Question

I am trying to do the validation rules in the client and I'm not getting, always goes to the post.

Always calls the post

_Layout.

<script src="@Url.Content("~/Content/scripts/jquery-1.8.3.min.js")" type="text/javascript"></script>        

<script src="~/Content/scripts/jquery-ui-1.8.24.min.js" type="text/javascript"></script>   

<script src="~/Content/scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>   

<script src="~/Content/scripts/jquery.validate.min.js" type="text/javascript"></script>   

<script src="~/Content/scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>   

Form:

@model FlexGestor.Models.UsuarioCadastro

@using (Html.BeginForm())
{


    <div class="row">
        @Html.TituloPagina("Informe seus dados para cadastro")    
        <div class="col-md-6">  
        @Html.LabelFor(m => m.Nome) @Html.ValidationMessageFor(m => m.Nome)    
        @Html.TextBoxFor(m => m.Nome, new { style = "width:250px;" })

    <div class="row">        
        <div class="col-md-6"> 
        <input class="btn btn-success" type="submit" value="Cadastrar" onclick="this.disabled=true;this.value='Cadastrando, por favor aguarde...';this.form.submit();"/>
        </div>
    </div>    
}

Web.config

  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />    
  </appSettings>
  <system.web>
Was it helpful?

Solution

This path won't mean anything to a web browser:

<script src="~/Content/scripts/jquery-ui-1.8.24.min.js" type="text/javascript"></script>

The ~ notation is a server-side concept, referring to the application root directory. The browser won't know what to make of that. So I'm guessing none of the JavaScript files referenced with this notation are being loaded.

You could use the workaround that you already have in place for another script tag:

<script src="@Url.Content("~/Content/scripts/jquery-1.8.3.min.js")" type="text/javascript"></script>

Or you could reference the files explicitly (though this would require a change for non-root deployments, so in general cases this isn't recommended):

<script src="/Content/scripts/jquery-ui-1.8.24.min.js" type="text/javascript"></script>

If you're using any recent version of ASP.NET MVC then I recommend constructing a script bundle, as that would handle the generation of the script tags (and even minification) for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top