asp.net mvc - Second time partialview not loading to div via from .ajax() in MVC4 -
i have issue loading partialview div second time. have checked previous posts in non of them helped me. posting issue here.
index.cshtml
<div id="divemailcontainer" style="display:block" class="row"> </div> _editemail.cshtml
<div class="row"> <div class="col-xs-12 col-sm-6 col-md-2 "> <input type="submit" value="save" class="btn btn-success width100per" /> </div> script type="text/javascript"> $(function () { $("#frmemail").validate({ rules: { ... submithandler: function (form) { $.ajax({ url: 'posteditemail', type: 'post', data: $(form).serialize(), success: function (result) { alert("in success"); $('#divemailcontainer').html(result); }, error: function (xhr, ajaxoptions, thrownerror) { alert(xhr.responsetext); alert(thrownerror); }, complete: function (xhr, textstatus) { alert(xhr.responsetext); alert(textstatus); } }); } }); controller
public partialviewresult posteditemail(string actiontype, formcollection col) { var communicationlocation = string.empty; .... return partialview("_editemail", memberemail); } first time partialview loading divemailcontainer error. if submit again partialview loading full post back. not calling submithandler.
only thing observed 1st time submit <form post /contactinformation/geteditemailbut when submit second time <form post /contactinformation/posteditemail.
what wrong?
update
second time forloop scriptblock loading. may issue forloop?
@using (html.beginscriptcontext()) { html.addscriptblock(
update
issue forloop htmlhelper, not ajax. secondtime script not loading. @russ cam can on this.
from experience putting script in partial leads inconsistent results. expecially script being inserted middle of page. highly recommend pull script partial , put on main page. since partial loaded after page load need tie script partial 1 of 2 ways.
1. tie events document
$(document).on('click', '.targetclass', function(){ //do stuff }); for example put id on input , change button
<input type="button" value="save" id="btnsave" class="btn btn-success width100per" /> your click event be
$(document).on('click', '#btnsave', function(){ //your ajax call here }); being tied document click event fire though input inserted document after load
put script in function called after partial loaded change
$(function () {
to
function partialscript(){ and call function after partial loaded
$('#divemailcontainer').html(result); partialscript();
Comments
Post a Comment