如何使用Jquery为我的asp.net MVC删除表中的已删除行

时间:2012-03-07 15:09:41

标签: asp.net-mvc-3 jquery

我有以下观点: -

<script type="text/javascript">
    $(document).ready(function () {
        $("#removerow").click(function () {
            $(this).remove();
        });
    });
</script>


<h2>Details</h2>

<fieldset>
    <legend>This Question Currently has @Model.Answers.Count() Answers/s. </legend>
<table id="incrementanswer">
    <tr>
        <th>
            Description
        </th>
        <th>
            Answer 
        </th>
        <th></th>
    </tr>

@foreach (var answer in Model.Answers.OrderBy(a=> a.IsRight))
{
       <tr id =  "removerow">

        <td>
            @Html.DisplayFor(modelItem => answer.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => answer.Answer_Description.description)
        </td>
        <td>
         @Ajax.ActionLink("Delete", "Delete", "Answer",
        new { id = answer.AnswersID },
          new AjaxOptions
          {
              Confirm = "Are You sure You want to delete this Answer ?",
              HttpMethod = "Post",
              UpdateTargetId = "toremove",
              OnSuccess = "removePartial2"
          })


        </td>
        </tr>
}
</table>
<div id = "progress2">
<img src= "@Url.Content("~/Content/images/ajax-loader1.gif") ">
</div>

包含一个脚本和一个ajax.actionlink,它在单击删除链接后调用此脚本。目前,只有当行是表中的第一行并且在确认删除之前删除该行时才会删除该行,那么可能是什么问题呢?

已编辑: - 我已将我的观点更新为以下内容,但我仍然无法强制removePartial2脚本删除已删除的行: -

</script>
<script type="text/javascript">
    function removePartial2() {
        $(this).remove();
}
</script>

}

<h2>Details</h2>

<fieldset>
    <legend>This Question Currently has @Model.Answers.Count() Answers/s. </legend>
<table id="incrementanswer">
    <tr>
        <th>
            Description
        </th>
        <th>
            Answer 
        </th>
        <th></th>
    </tr>

@foreach (var answer in Model.Answers.OrderBy(a=> a.IsRight))
{
       <tr id =  @answer.AnswersID>

        <td>
            @Html.DisplayFor(modelItem => answer.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => answer.Answer_Description.description)
        </td>
        <td>
         @Ajax.ActionLink("Delete", "Delete", "Answer",
        new { id = answer.AnswersID },
          new AjaxOptions
          {
              Confirm = "Are You sure You want to delete this Answer ?",
              HttpMethod = "Post",
              UpdateTargetId = @answer.AnswersID.ToString(),
              OnSuccess = "removePartial2"
          })


        </td>
        </tr>
}
</table>
<div id = "progress2">
<img src= "@Url.Content("~/Content/images/ajax-loader1.gif") ">
</div>

1 个答案:

答案 0 :(得分:0)

  1. “Ids。必须。是。独一无二。”

  2. 如果单击ROW,则有一个删除行的函数,而不仅仅是删除选项。我会删除你的第一个javascript块,只使用ajax调用函数。

  3. OnSuccess是Ajax调用是否返回200 code。这只是在ajax调用成功的情况下 - 它不会检查返回的值是否正确。