This is a question that follows on from my previous question; MVC: Button not firing click event in a tablecell
Because I am creating a grid dynamically, I have to use a jquery delegate in order to attach an event to a button in the grid.
So I have this section in my view;
<section id="rightSide" class="shrinkwrap" style="float: right;margin-top:10px;width:500px;">
<fieldset>
<legend>Select Other Materials</legend>
<form method="get" action="@Url.Action("CreateOtherMaterials", "DataService")"
data-scd-ajax="true" data-scd-target="#otherMaterialList">
<p>Select a Material: <input type="search" name="searchOtherMaterial" id="searchOtherMaterial" data-scd-autocomplete="@Url.Action("AutocompleteOtherMaterial", "DataService")" style = "width: 300px;" class="submitOtherMaterialSelectionNew" data-scd-add-other-material="@Url.Action("AddOtherMaterialNew", "DataService")"/>
@Html.DialogFormButton("Add New Material", Url.Action("AddMaterial", "Popup"), "Add New Material", null, Url.Action("Create"))
</p>
</form>
@Html.Partial("_OtherMaterials", Model.SupplierMaterialList.Where(x => x.PrimaryMaterialFlag == false).ToList())
</fieldset>
</section>
The Partial View _OtherMaterials looks like
@model IList<SupplierMaterial>
<div id="otherMaterialList" >
<p>These materials have now been added for this supplier</p>
<table id="tableOtherSupplierList">
@Html.DisplayForModel()
</table>
</div>
And my display template;
@model SupplierMaterial
<tr>
<td>@Model.Material.MaterialName </td>
<td>
<form class="shrinkwrap" method="get" action="@Url.Action("RemoveOtherMaterial", "DataService", new { id = Model.MaterialId })">
<input type="button" id="btnRemove" value="Remove" class="removeItem"/>
</form>
</td>
</tr>
I use this query remove a row in the grid when the Remove button is clicked;
$('body').delegate('.removeItem', 'click', function () {
var $form = $(this).closest("form");
var options = {
url: $form.attr("action"),
type: $form.attr("method")
};
$.ajax(options).done(function (data) {
$(this).parent("tr").remove();
});
return false;
});
I tried replacing the body element with something closer such as #tableOtherSupplierList but that did not work. I would like to use on() instead of delegate but so far only the above code seems to work. The main issue I have however is that the line $(this).parent("tr").remove(); does not modify my view and remove the tablerow. How do I fix this?