Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic partial view + jquery form hijack + client validation = not working
    text
    copied!<p>I'm using MVC 3 with unobtrusive javascript for client validation.</p> <p>I have a table with rows which are clickable. When clicked I want to bring up a dynamically loaded partial view. This is the code for this:</p> <pre><code>function GetStuff(id) { $.ajax( { url: "Edit/" + id, success: function (result) { $("#DivTest").html(result); } }); } </code></pre> <p>This far everything works. The problem is when I'm trying to save something in the partial using jquery. The form is hijacked, like this:</p> <pre><code>$(function () { $.post($(this).attr("action"), $(this).serialize(), function (data) { alert("test"); }); e.preventDefault(); }); </code></pre> <p>This all together makes the client validation not work. It just makes the post even if I have left out some required values. This all works if I'm not using ajax to post or if the partial is loaded on page load (not dynamic).</p> <hr> <p>What I've tried:</p> <p>I tried putting this before the post:</p> <pre><code>if($('form').validate().form()){ ... } </code></pre> <p>This just returns true every time though..</p> <p>From this post i got 2 suggestions <a href="https://stackoverflow.com/questions/2652586/asp-net-mvc-2-loading-partial-view-using-jquery-no-client-side-validation">ASP.NET MVC 2 loading partial view using jQuery - no client side validation</a></p> <p>Adding </p> <pre><code>Sys.Mvc.FormContext._Application_Load(); </code></pre> <p>after the partial has been loaded.. And set the datatype of the post to "html". Neither worked.</p> <p>I also tried this method with no luck: <a href="http://www.deepcode.co.uk/2010/08/mvc-ootb-validation-when-pulling-in.html" rel="nofollow noreferrer">http://www.deepcode.co.uk/2010/08/mvc-ootb-validation-when-pulling-in.html</a></p> <p>This combination of techniques are something very common I guess, why is it so hard to make it work? Any help is highly appreciated. Thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload