我正在工作一个asp.net MVC Web应用程序,在我看来,我有一个如下的webgrid:-
@model T.ViewModels.GridList<T.Models.Resources>
var gridcolumns = new List<WebGridColumn>();
gridcolumns.Add(new WebGridColumn()
{
CanSort = false,
Format =
(item) =>
{
var banner = item.Value as T.Models.Resources;
return Ajax.ActionLink("Delete", "Delete", banner.Technology.TechnologyType.Name, new { id = banner.TechnologyID },
new AjaxOptions
{
Confirm = "Are You sure You want to delete (" + banner.Technology.Tag.ToString() + ")",
HttpMethod = "Post",
OnSuccess = "deletionconfirmation",
OnFailure = "deletionerror"
});
}
});
var grid = new WebGrid(
canPage: true,
rowsPerPage: Model.PageSize,
canSort: true,
ajaxUpdateContainerId: "grid");
grid.Bind(Model.Content, rowCount: Model.TotalRecords, autoSortAndPage: false);
grid.Pager(WebGridPagerModes.All);
@grid.GetHtml(htmlAttributes: new { id = "grid" }, // id for ajaxUpdateContainerId parameter
fillEmptyRows: false,
tableStyle: "table table-bordered table-hover",
mode: WebGridPagerModes.All,
columns: gridcolumns
);
现在,网络网格将生成带有行的HTML表格。如我上面的示例代码所示,我有一个Ajax.ActionLink
来删除一行,并且我正在传递项目ID。然后在OnSuccess = "deletionconfirmation"
函数内部,我将删除相关行,如下所示:-
function deletionconfirmation(data)
{
if (data.IsSuccess == "reload") {
location.reload();
}
if (data.IsSuccess == "True") {
$('#' + data.id).remove();
$.fn.jAlert({
'title': 'Deletion Confirmation',
'message': data.description + ' deleted successfully',
'theme': 'success',
'clickAnywhere': false
});
}
但是我的问题是如何将WebGrid内的项目ID添加为生成的行的ID?
答案 0 :(得分:1)
this
方法内的deletionconfirmation
表达式将为您提供锚点HTML元素用户单击的锚标记,以启动ajax调用。您可以使用jQuery closest
方法来获取其外部TR。
function deletionconfirmation(data) {
var $tr = $(this).closest("tr");
//Do your if conditions to check the data property to determine what to do next.
$tr.fadeOut(400);
}