使用AJAX调用的结果更新表行

时间:2014-03-19 13:43:21

标签: jquery html ajax asp.net-mvc

我有一个局部视图,其中包含一个表格行及其内容......

@{string divid = "lostbusiness_" + Model.lostid;}
<tr id="@divid">
    @using (Ajax.BeginForm(
        "UpdateLostBusiness",
        new AjaxOptions
        {
            HttpMethod = "POST",
            OnSuccess = "lostbusinessupdated"
        }))
        {
        <td>
            <script type="text/javascript">
                function lostbusinessupdated(result) {

                    $("#@divid").html(result);
                }
            </script>
            @Html.Hidden("lostid", Model.lostid)
            @Html.Hidden("bookingid", Model.bookingid)

            @Ajax.ActionLink("Delete",
                    "DeleteLostBusiness", new { lostid = Model.lostid },
                    new AjaxOptions
                    {
                        Confirm = "Delete?",
                        HttpMethod = "POST",
                        OnSuccess = "postmessage('lostbusiness_Deleted_" + Model.lostid + "')"
                    })
        </td>
        <td>
            @Html.DropDownListFor(model => model.lost_code, (SelectList)ViewBag.ddllostcodes, "--Select Users--")

        </td>
        <td>@Html.TextBox("lost_reason", Model.lost_reason)<input type="submit" value="update" /></td>



    }
</tr>

成功的ajax调用后,上面的部分视图返回到主视图。 我不知道在哪里插入结果。由于局部视图包含表格行,因此表格行是重复的,因此生成的html包含具有相同ID的嵌套表格行。

我会在部分视图之外移动表格行开始和结束标记,但这会导致插入ajax函数出现问题。

1 个答案:

答案 0 :(得分:1)

JQuery方法replaceWith() (JQuery replaceWith docs)

怎么样?

&#34;用提供的新内容替换匹配元素集中的每个元素,并返回已删除的元素集。&#34; 所以:

$("#@divid").replaceWith(result);