C# 使用ajax mvc3.net将参数传递给jquery
我正在为某个站点构建后台,这里我需要使用ajax从数据库中删除评论,问题是我无法将删除后要隐藏的id传递到ajax中的jquery。以下是评论的代码:C# 使用ajax mvc3.net将参数传递给jquery,c#,jquery,ajax,asp.net-mvc-3,C#,Jquery,Ajax,Asp.net Mvc 3,我正在为某个站点构建后台,这里我需要使用ajax从数据库中删除评论,问题是我无法将删除后要隐藏的id传递到ajax中的jquery。以下是评论的代码: <% foreach (var item in Model) { %> <div class="row" id = "row<%= item.CommentID %>"> <div class="editor-label detail">Comment</div>
<% foreach (var item in Model) { %>
<div class="row" id = "row<%= item.CommentID %>">
<div class="editor-label detail">Comment</div>
<div class="form-element"><%: Html.DisplayFor(modelItem => item.Text) %></div>
<div class="editor-label detail">Date</div>
<div class="form-element"><%: Html.DisplayFor(modelItem => item.Date) %></div>
<div class="form-element">
<div class="button delete"><%: Ajax.ActionLink("Delete", "Deletecomment", new { id = item.CommentID }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "deleteRow" })%></div>
</div>
</div>
<% } %>
这是我的jquery
function deleteRow() {
var url_ = this.url.split("/");
$("#row" + url_[url_.length - 1]).remove();
}
我所做的是获取url并将其拆分,以获取db中条目的id,然后将其作为id添加到表单中,然后删除该id,但我确信有更好的解决方案您不能动态地将id传递给回调:
new AjaxOptions { HttpMethod = "POST", OnSuccess = "function(){ return deleteRow('" + item.CommentID + "');" }
function deleteRow(id) {
$("#row" + id).remove();
}
<%= Ajax.ActionLink(
"Delete",
"Deletecomment",
new {
id = item.CommentID
},
new AjaxOptions {
HttpMethod = "POST",
OnSuccess = "deleteRow('#row" + item.CommentID + "')"
}
) %>
假设您使用的是jquery低调的ajax脚本,则可以将参数传递给回调:
new AjaxOptions { HttpMethod = "POST", OnSuccess = "function(){ return deleteRow('" + item.CommentID + "');" }
function deleteRow(id) {
$("#row" + id).remove();
}
<%= Ajax.ActionLink(
"Delete",
"Deletecomment",
new {
id = item.CommentID
},
new AjaxOptions {
HttpMethod = "POST",
OnSuccess = "deleteRow('#row" + item.CommentID + "')"
}
) %>