我有一个显示已订阅论坛主题的列表,并且在每一行中都有一个取消订阅的按钮。我使用 Ajax 取消订阅工作正常,但我想使用 Ajax 刷新以从列表中删除未订阅的线程。谁能建议我。我的代码如下:
使用 Ajax 的想法是因为我只想刷新列表部分而不是整个页面。
索引.aspx
<% foreach (var thread in Model.threads) { %>
<tr>
<td><%: thread.Title %></td>
<td><%: Ajax.ActionLink("Unsubscribe", "ToggleForumAlertFromList",
new { id = thread.ForumId },
new AjaxOptions { HttpMethod = "POST",
UpdateTargetId = "none",
new { ID = "togglealertlink" })%>
</td>
</tr>
<% } %>
Controller
[HttpPost]
public ActionResult ToggleThreadAlertFromList(Guid id)
{
// Do stuff to unsubscribe
return new EmptyResult();
}
最佳答案
您需要连接一个在 POST 完成时触发的 OnSuccess 回调。
此回调可以重新查询您的列表或简单地从您的列表中删除未订阅的项目。
<% foreach (var thread in Model.threads) { %>
<tr id= "<%: thread.ForumId %>">
<td><%: thread.Title %></td>
<td><%: Ajax.ActionLink("Unsubscribe", "ToggleForumAlertFromList",
new { id = thread.ForumId },
new AjaxOptions { HttpMethod = "POST",
UpdateTargetId = "none",
OnSuccess = "removeItem('thread.ForumId')"
new { ID = "togglealertlink" })%>
</td>
</tr>
<% } %>
function removeItem(itemId) {
$("#" + itemId).remove();
}
这假设每个 parent <tr>
元素已被分配了一个唯一的 forumId,如上所示。
关于c# - MVC3 和 Ajax : Refresh list to hide a record which was deleted from list by a button click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31050594/