0

How can i trigger the submit button from partial view without using Html.BeginForm tag in Partial View. Reason being when there are two form tags, that is in main view and partial view, the validation doesn't work.

The important thing is that I want the validation to work.

My Partial View code:

<div id="AddMe">
    <div class="row">
        @Html.LabelFor(model => model.FirstName)
        @Html.TextBoxFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName) // this should work
    </div>     
</div>
<div>
    <input name="submit" type="submit" id="submit" value="Save" /> // how do i trigger this from partial view
</div>

My Main View code:

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
    <div class="modal" id="modalId" role="dialog">
     <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">

            </div>
            <div class="modal-body" id="modalbodyId">
                @Html.Partial("PartilViewName")
            </div>
            <div class="modal-header">
                 <button type="submit">  
            </div>
        </div>
    </div>
   </div>
}

For Validation purpose, I have added the following lines of code in Main View.    

$("#modalId").click(function () {
        var form = $("#modalbodyId").closest("form");
        form.removeData('validator');
        form.removeData('unobtrusiveValidation');
        $.validator.unobtrusive.parse(form);

        $.ajax({
            url: "/ControllerName/ActionName",
            type: "POST",
            data: $("#modalbodyId").serialize(),
            cache: false
        });


    });

Also just for the records all the models and other Annotations are in place.

Note: Do i need to add the unobtrusive/ajax code inside Partial View.

user2322507
  • 141
  • 1
  • 5
  • 19
  • this link may help http://stackoverflow.com/questions/5785510/how-to-trigger-validation-without-using-a-submit-button. on your question about the ajax code inside the Partial view the answer is no. Partial views are inserted into the middle of a page and you don't want your script inserted there. – Matt Bodily Aug 27 '14 at 19:28
  • Not clear what you are trying to do here. There is no need to parse the validator (this is done when the view is loaded, which includes the partial), and why do you have a `` in your partial when you appear to be wanting to post using AJAX? –  Aug 28 '14 at 02:23

0 Answers0