Reputation: 449
I have a partial view that opens a dialog box for entering some data. Once the button is clicked, an Ajax call is performed to reset that partial view and another. When I step through the code, everything seems to work alright except that when I open the partial view back up it the data is still in the box. I found some success following this question Caching issue with loading partial views into JQuery dialogs but when I implement that the button doesn't work the second time I use it. My code is below:
jQuery and Ajax that refreshes the view and view that contains other partial views:
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$('#ChangeHeatName').click(function (e) {
var tdata = $('#form1').serialize();
var origname = $('#HeatNameDiv').find('input[name="heatName"]').first().val();
var newname = $('#HeatNameDiv').find('input[name="updatedHeat"]').first().val();
$.ajax({
type: "POST",
data: {
mCollection: tdata,
heatName: origname,
updatedHeat: newname
},
url: '@Url.Action("ChangeHeatName","Home")',
//url: "/Home/ChangeHeatName",
success: function (result) { success(result); }
});
});
function success(result) {
$('#HeatNameDiv').dialog('close');
//Ajax with problem
$.ajax({
type: "GET",
url: '@Url.Action("ButtonsPartial","Home")',
success: function (result2) { $("#ButtonsPartial").html(result2); }
});
$("#Partial_Chem_Analysis").html(result);
}
});
</script>
<section>
<div id ="ButtonsPartial" title="ButtonsPartial">
@Html.Action("ButtonsPartial", "Home")
</div>
<div id="Partial_Chem_Analysis" title="Partial_Chem_Analysis">
@Html.Action("PartialChemAnalysis", "Home", Model)
</div>
</section>
<section>
The partial view:
@using System.Data;
@using System.Dynamic;
@using System.Collections.Generic;
@using System.Linq;
@model TheManhattanProject.Models.ButtonsModel
<div id="Chem_Buttons">
<h2 class="alignleft">Chemistry Table</h2>
<p class="alignright">Heat Name<input type="text" name="heat_name" value="@Model.heatName" class="search-query" placeholder="Search" style ="width:100px" readonly="true"/>
<button class="btn btn-success" id="Change_Heat_Name" value="Change_Heat_Name" name="action:Change_Heat_Name" type="button"> Change Heat Name</button>
Grade<input type="text" name="heat_grade" value="@Model.grade" class="search-query" placeholder="Search" style="width:100px" readonly="true"/>
<button class="btn btn-success" id="ChangeHeatGrade" value="ChangeHeatGrade" name="action:Change_Grade" type="button">Change Grade</button>
Delete Record<input type="text" name="delete_record" value="@Model.heatName" class="search-query" placeholder="Search" style ="width:100px" readonly="true"/>
<button class="btn btn-success" id="DeleteRecord" value="DeleteRecord" name="action:Delete_Record" type="button">Delete Record</button>
</p>
<div style="clear: both;"></div>
</div>
<div id="HeatNameDiv" title="Change Heat Name">
@using (Html.BeginForm("ChangeHeatName", "Home", "POST"))
{
<section>
Heat Name:<input type="text" name="heatName" value="@Model.heatName" style ="width:100px" readonly="true"/>
//Value staying to what was entered not ""
Change to:<input type="text" name="updatedHeat" value="" style="width: 100px" />
<input type="button" id="ChangeHeatName" name="ChangeHeatName" value="Change" />
</section>
}
</div>
<div id="HeatGradeDiv" title="Change Heat Grade">
@using (Html.BeginForm("ChangeGrade", "Home", "POST"))
{
<section>
Heat Grade:<input type="text" name="grade" value="@Model.grade" style ="width:100px" readonly="true"/>
Change to:<input type="text" name="updatedGrade" value="" style="width: 100px" />
<input type ="hidden" name="hiddenHeat" value ="@Model.heatName)" />
<input type="button" id="ChangeGrade" name="ChangeGrade" value="Change" />
</section>
}
</div>
<div id="DeleteRecordDiv" title="Delete Record">
@using (Html.BeginForm("DeleteRecord", "Home", "POST"))
{
<section>
Heat Name:<input type="text" name="heatName" value="@Model.heatName" style ="width:100px" readonly="true"/>
<input type="button" id="DeleteRecordBtn" name="DeleteRecordBtn" value="Delete" />
</section>
}
</div>
Upvotes: 3
Views: 1374
Reputation: 14250
The event handler was tied to the original button that got replaced with a partial view load. After that it doesn't have a handler until you register it again or register the click handler to a parent DOM element the first time.
$(document).ready(function () {
// instead of this
$('#ChangeHeatName').click(function (e) {
...
});
// do this
$("body").on("click", "#ChangeHeatName", function(e) {
...
});
});
Try to replace "body"
with a static DOM element as close to your reloaded partial as possible.
$("#ButtonsPartial").on("click", "#ChangeHeatName", function(e) {
...
});
Upvotes: 1