如何从后端和前端删除特定的表行?

时间:2016-11-22 10:07:34

标签: jquery asp.net ajax asp.net-mvc-4 razor

我有一个Ajax.ActionLink向服务器发送请求并从服务器获取结果。所以在AjaxOption中我只写了成功函数的名称。

OnSuccess = "SuccessDeleteFunction"

但基于结果我想改变DOM元素。为了在成功函数中传递当前DOM元素,我使用了这段代码:

OnSuccess = "SuccessDeleteFunction(this)"

但我怎样才能同时拥有两者。

function SuccessDeleteFunction(result, tag) {

    if (result.Success) {
        //    do sth
    }
    eval(result.Script);
}

这是我的代码:

@foreach (var item in Model)
{
    <tr class="ContentRow">
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Ajax.ActionLink(" ", "Delete", "Product", new { id = @item.Id }, new AjaxOptions() { Confirm = "Are you sure?", HttpMethod = "get", OnSuccess = "SuccessDeleteFunction" }, new { @class = "btn fa fa-trash text-danger fa-2x " })

        </td>
     </tr>
}

1 个答案:

答案 0 :(得分:0)

您必须将项目ID改为TR:

@foreach (var item in Model)
    {
        <tr class="ContentRow" id="tr_'@item.id'">
             <td>
                 @Html.DisplayFor(modelItem => item.Name)
             </td>
              <td>
                 @Html.DisplayFor(modelItem => item.Email)
              </td>
               <td>
                 @Ajax.ActionLink(" ", "Delete", "Product", new { id = @item.Id }, new AjaxOptions() { Confirm = "Are you sure?", HttpMethod = "get", OnSuccess = "SuccessDeleteFunction" }, new { @class = "btn fa fa-trash text-danger fa-2x " })

              </td>
        </tr>
   }

在您的JS代码中,您必须执行此操作,而您需要更多的内容是从ID

返回您的项SERVER RESPONSE
function SuccessDeleteFunction(result) {

    if (result.Success) {
        // also return your that id from server in response....
        $("tr_" + result.id).remove();
    }
    eval(result.Script);
}