Jquery 如何在单击删除链接后从我的razor视图中删除对象
我的asp.net MVC中有以下视图:-Jquery 如何在单击删除链接后从我的razor视图中删除对象,jquery,ajax,asp.net-mvc-3,Jquery,Ajax,Asp.net Mvc 3,我的asp.net MVC中有以下视图:- @foreach (var item in Model) { <tr > <td> @Html.DisplayFor(modelItem => item.UserID) <td id ="userclass"> @Ajax.ActionLink("Add Use
@foreach (var item in Model) {
<tr >
<td>
@Html.DisplayFor(modelItem => item.UserID)
<td id ="userclass">
@Ajax.ActionLink("Add User to Class",
"Register", "User",
new { id = item.UserID, classid = ViewBag.id },
new AjaxOptions {
InsertionMode = InsertionMode.InsertAfter,
HttpMethod = "POST",
UpdateTargetId = "incrementadd",
LoadingElementId = "progressToadd",
OnSuccess = "removerow"
})
</td>
那么我如何才能做到这一点呢?
BR您的问题有点奇怪,因为您正在谈论删除一行并向我们展示有关添加类的代码。然后您将讨论
removerow
回调。在你的问题中,事情并不是真正相关的。但无论如何,我想到了两种可能性:
<div id="incrementadd">
@Html.Partial("_Table", Model.Answers)
</div>
对于第二种方法,必须将要删除的表的当前行传递给成功回调。为此,您可以在
元素上为其分配一个唯一的id:
@foreach (var item in Model)
{
<tr id="row@item.ID">
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td id ="userclass">
@Ajax.ActionLink(
"Delete answer",
"Delete",
"Answers",
new {
id = item.ID,
},
new AjaxOptions {
HttpMethod = "POST",
OnSuccess = string.Format(
"deleteSuccess({0})",
Json.Encode(item.ID)
),
LoadingElementId = "progressToadd"
}
)
</td>
</tr>
}
很抱歉不一致,我已经编辑了我的问题以包含正确的代码。我想做的是删除添加而不是删除的对象;但是当我定义表行的ID时,我应该使用@row吗?我尝试了以下方法,效果很好。那么,使用tr id=”之间有什么区别呢row@item.ID“>这将取决于这些ID的格式。如果它们是数字,如果您使用
id=“123”
,您将得到无效的HTML,因为id不能以数字开头,而id=“row123”确保您得到有效的HTML。但是在我的应用程序中的某些视图上,我使用了类似的内容,它工作正常,尽管答案id将具有诸如“123”之类的值,因为它是一个同一整数。。。
[HttpPost]
public ActionResult Delete(int id)
{
1. Delete the record with the given id in your database
2. Refetch all the answers and pass them to the partial
return PartialView("_Table", answers);
}
@foreach (var item in Model)
{
<tr id="row@item.ID">
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td id ="userclass">
@Ajax.ActionLink(
"Delete answer",
"Delete",
"Answers",
new {
id = item.ID,
},
new AjaxOptions {
HttpMethod = "POST",
OnSuccess = string.Format(
"deleteSuccess({0})",
Json.Encode(item.ID)
),
LoadingElementId = "progressToadd"
}
)
</td>
</tr>
}
var deleteSuccess = function(rowid) {
$('#row' + rowid).remove();
};