Validation not working on partial views
I have a root view with two partial views that get loaded into divs based on a button click. Here's the gist of the root view:
@using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Donation information</legend> <p> @Html.LabelFor(m => m.DonationType.DonationTypex) <br /> @Html.DropDownListFor(model => model.DonationType.DonationTypeID, ViewBag.DonationTypes as SelectList, htmlAttributes: new { @id = "selectList" }) @Html.ActionLink("Select", "ContinueDonation", "Donation", htmlAttributes: new { @id = "btnSelect1" }) </p> <div id="donationSection1"></div> <br /> <input id="btnAddPayment1" type="button" value="Add Payment Info" /> <br /><br /> <div id="paymentSection1"></div> <input type="submit" id="btnSubmitDonation" value="Save" /> <input type="button" id="btnCancelDonation" value="Cancel" /> </fieldset> }
The partial views that are loaded into the donationSection1 and paymentSection1 divs share the same Model, just contain editors, labels etc for different properties within it... just wondering how I can get it to validate the partial views... if the submit/save button is on the root view... thanks!
...jquery that loads div... (controller returns partial view sharing same model):
$("#btnSelect1").click(function () { var donationTypeID = $(this).closest('p').find('#selectList').val(); var id = parseInt(donationTypeID); var route = '/Donation/ContinueDonation?dTypeId=' + id; $("#donationSection1").load(route, function () { $("#donationSection1").show('slow'); }); return false; });
I guess my next question would be how to best go about posting back the data all at once to the controller...
Answers
try using jquery validator.unobtrusive.parse
$("#btnSelect1").click(function () { var donationTypeID = $(this).closest('p').find('#selectList').val(); var id = parseInt(donationTypeID); var route = '/Donation/ContinueDonation?dTypeId=' + id; $("#donationSection1").load(route, function () { $("#donationSection1").show('slow'); }); jQuery.validator.unobtrusive.parse('#donationSection1') return false; });
here is the link with more explanations
http://itmeze.com/2010/10/08/client-side-validation-after-ajax-partial-view-result-in-asp-net-mvc-3/