您可以使用属性来保存item.Id
并在事件处理程序中获取它。另外你不需要使用window.location.href
CSHTML
@foreach (var item in Model)
{
<tr class="deleteItem" data-url="@Url.Action("Action", new RouteValueDictionary { { "id", item.Id } })">
<td>@Html.DisplayFor(x => item.Field1)
</td>
<td>@Html.DisplayFor(x => item.Field2)
</td>
</tr>
}
JavaScript
$('tr.deleteItem').click(function () {
$.ajax({
url: $(this).data("url"),
type: 'GET',
cache: false,
success: function (result) {
$('#partialView_div').html(result);
}
});
return false;
});
OR
CSHTML
@foreach (var item in Model)
{
<tr data-item-id="@item.Id" class="deleteItem">
<td>@Html.DisplayFor(x => item.Field1)
</td>
<td>@Html.DisplayFor(x => item.Field2)
</td>
</tr>
}
JavaScript
$('tr.deleteItem').click(function () {
var url = '@Url.Action("Action", "Controller", { "id" = "???" })';
url = url.replace("???", $(this).data("item-id"));
$.ajax({
url: url,
type: 'GET',
cache: false,
success: function (result) {
$('#partialView_div').html(result);
}
});
return false;
});