[RESOLVED]unobtrusive client side validation not working when using ajax.beginform?

Hi,

Why does unobtrusive client side validation not working when using ajax.beginform? I am using MVC 3 and have the following jquery libraires declared yet when submitting an Ajax.BeginForm the unobtrusive validation doesnt work.

@section scripts {
    @Content.Script("jquery.validate.min.js", Url)
    @Content.Script("jquery.validate.unobtrusive.min.js", Url)
    @Content.Script("jquery.unobtrusive-ajax.min.js", Url)
  
}

  @using (Ajax.BeginForm("AddComment", "Blog", new AjaxOptions
        {
            HttpMethod = "POST",
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "blogComments",
            LoadingElementId = "progress"

        })

        )
    {
       
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.Blog.BlogID)
     

        <div>
            <div>
                Add Comment 
            </div>
            <div>
                @Html.TextAreaFor(model => model.BlogComment.Comment, 1, 100, new { id = "commentbox" })
                @Html.ValidationMessageFor(model => model.BlogComment.Comment)
            </div>
        </div>
        
        <div >
              <input type="submit" id="button" value="Add" />
             
        </div>
            
        
       
    }

In my web config I also have the following enabled:

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

Isa this a partial view rendered in other main view?

if so, how r u rendering?

either through @Html.Partial() or loading on on click of something?

One of the things about validation is that you have a choice in how things are displayed:

 @Html.ValidationSummary(true)

excludePropertyErrorsType: System.Boolean
true to have the summary display model-level errors only, or false to have the summary display all errors.

Try false and let us know what happens.

the Ajax.beginform declaration is in a main view and the form fields are in partial view called using  @Html.Partial("_CreateComment", Model)

Hi GStar99,

Thanks for your post.

GStar99

unobtrusive client side validation not working when using ajax.beginform?

what’s error it renders?You can add script tags in LayOut.cshtml instead of index.cshtml page

 jquery.unobtrusive-ajax.min.js.

More information:

#Unobtrusive AJAX Form Validation in ASP.NET MVC

http://www.codeproject.com/Articles/460893/Unobtrusive-AJAX-Form-Validation-in-ASP-NET-MVC

If there’s anything else I can do for you regarding this matter,please feel free to post this forum.

Best Regards,

Eileen

Hi,

I’ve moved the script to Layout.cshtml and it still doesnt work?

jquery.unobtrusive-ajax.min.js

Hi,

what’s error?Did you add all scripts to layout.cshtml?

Leave a Reply