【问题标题】:How to delete specific table row from backend and frontend?如何从后端和前端删除特定的表行?
【发布时间】:2017-04-05 22:59:20
【问题描述】:

我有一个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>
}

【问题讨论】:

  • 您不能将参数传递给 ajax 成功方法。你为什么需要它?它没有任何意义。
  • 我想要如果结果是成功然后删除父 tr。所以不需要从服务器重新加载列表,在客户端我会处理它。
  • 也分享你的html。
  • 当您制作 html 时,向您要删除的所有标签 TR 添加一个 ID,并在成功方法上选择该 TR(具有 ID 的特定 TR)并将其删除。分享您的 html,以便我在代码方面为您提供帮助。
  • 好,还可以添加您的 TR,在此点击时将其删除。

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


【解决方案1】:

你必须将你的物品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 代码中,您必须这样做,您还需要做的一件事是从SERVER RESPONSE 退回您的物品ID

function SuccessDeleteFunction(result) {

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

【讨论】:

    猜你喜欢
    • 2021-09-20
    • 1970-01-01
    • 2021-08-15
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2010-11-12
    • 2023-03-16
    相关资源
    最近更新 更多