Nick B
Nick B

Reputation: 715

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:

@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" })
        &nbsp;&nbsp;
        @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...

...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;
    });

Upvotes: 0

Views: 1373

Answers (1)

COLD TOLD
COLD TOLD

Reputation: 13599

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/

Upvotes: 1

Related Questions