Whenever I tried to render partial view on bootstrap modal, the ajax request which is rendering the partial view is getting called multiple times. I am making ajax request on button click which is on the Main view.
Main View :- While rendering the main view I have rendered the partial view also like below
<div class="modal fade" id="surveyPreviewModal" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="surveyPreviewLabel" aria-hidden="true"> <div class="modal-lg modal-dialog"> <div class="modal-content" id="surveyPreviewContent"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="surveyPreviewLabel">Survey Preview</h4> </div> <div class="modal-body" id="surveyPreviewBody"> @Html.Partial("_SurveyPreview",new LMS_TraineeSurveyPaginationViewModel()) </div> </div> </div> </div> and this is the button which is on main view rendering the partial view in modal
<button id="btnSurveyPreview" class="btn btn-primary" data-toggle="modal" data-target="#surveyPreviewModal" data-surveyID="@ViewBag.SurveyID"> Survey Preview </button> In partial view I have written the below code. On bootstrap modal show event I am calling SurveyPreview(js function) written in partial view.
@model LMS_TraineeSurveyPaginationViewModel $(document).ready(function () { $('#surveyPreviewModal').on('show.bs.modal', function (e) { surveyID = $(e.relatedTarget).attr('data-surveyID'); SurveyPreview(@SurveyPageTypePageNumber.StartPage,null); }); }) And in SurveyPreview function I am making ajax request as follows
function SurveyPreview(page,btnID) { ....Get SurveyID and page code..... $.post('@Url.Action("SurveyPreview", "Survey")', { SurveyID : surveyID, page : page }, function (data) { $('#surveyPreviewBody').html(''); $('#surveyPreviewBody').html(data); SetProgressBar(page,'@(Model==null?0: Model.Pager.TotalPages)'); }).fail(function () { alert("error in GetTraineeSurvey"); }).success(function () { }); } And from SurveyPreview controller method I am returning the same partial view with model and appending the results to modal body.
public PartialViewResult SurveyPreview(int SurveyID, int page) { --- some code-- return PartialView("_SurveyPreview", viewModel); } Lets I clicked on button for the first time, then modal is getting opened and after closing and re-opening it the ajax request made twice, then again closing and re-opening ajax request made thrice. Every time the counter is getting increased
Also I have Next/Prev buttons on partial view which is rendering the same partial view.
Is the correct sequence of events happening here ? Or am I missing something here ?
Any help on this appreciated !