如何通过ajax actionlink向表添加行

时间:2013-09-02 18:58:43

标签: ajax asp.net-mvc-4

我是MVC4的新手,我遇到了问题。 当我想在ajax中的表中添加一行时,它似乎不像我想要的那样。 这是我的代码

<table>
            <thead>
                <tr>
                    <th>Tên học sinh </th>
                    <th>Giáo lý viên </th>
                    <th>Năm Học</th>
                    <th>Điểm TB Học Kỳ 1 </th>
                    <th>Điểm TB Học Kỳ 2 </th>
                    <th>Điểm Tổng Kết Năm Học </th>
                    <th>Lớp </th>
                </tr>
            </thead>

            @using (Html.BeginForm())
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)

                <fieldset class="form-horizontal">
                    <legend></legend>

                    <tbody>
                        <tr>
                            <td>
                                @Html.DropDownList("HocSinhId", String.Empty)
                                @Html.ValidationMessageFor(model => model.HocSinhId, null, new { @class = "help-inline" })
                            </td>
                            <td>
                                @Html.DropDownList("GiaoLyVienId", String.Empty)
                                @Html.ValidationMessageFor(model => model.GiaoLyVienId, null, new { @class = "help-inline" })
                            </td>
                            <td>
                                @Html.TextBoxFor(model => model.NamHoc)
                                @Html.ValidationMessageFor(model => model.NamHoc, null, new { @class = "help-inline" })
                            </td>
                            <td>
                                @Html.EditorFor(model => model.DiemTBHK1)
                                @Html.ValidationMessageFor(model => model.DiemTBHK1, null, new { @class = "help-inline" })
                            </td>
                            <td>
                                @Html.EditorFor(model => model.DiemTBHK2)
                                @Html.ValidationMessageFor(model => model.DiemTBHK2, null, new { @class = "help-inline" })
                            </td>
                            <td>
                                @Html.EditorFor(model => model.DiemTongKetNamHoc)
                                @Html.ValidationMessageFor(model => model.DiemTongKetNamHoc, null, new { @class = "help-inline" })
                            </td>
                            <td>
                                @Html.DropDownList("LopId", String.Empty)
                                @Html.ValidationMessageFor(model => model.LopId, null, new { @class = "help-inline" })
                            </td>
                        </tr>
                        <div id="addDetails"></div>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td>
                                @Ajax.ActionLink("Thêm Chi Tiết", "AddDetails", new AjaxOptions
                            {
                                UpdateTargetId = "addDetails",
                                InsertionMode = InsertionMode.InsertAfter,
                                HttpMethod = "GET",

                            })
                            </td>

                        </tr>
                        <tr>
                            <td colspan="6">
                                @*<a  href="#"> </a>*@
                                <input type="submit" value="Lưu thông tin" class="button" />
                            </td>
                        </tr>
                    </tfoot>

                    <div class="form-actions no-color">

                    </div>
                </fieldset>
            }

        </table>

然后我调用ajax加载更多行,如果需要这里是AddDetails文件,这将是InsertAfter addDetails id。

<tr>
<td>
    @Html.DropDownList("HocSinhId", String.Empty)
    @Html.ValidationMessageFor(model => model.HocSinhId, null, new { @class = "help-inline" })
</td>
<td>
    @Html.DropDownList("GiaoLyVienId", String.Empty)
    @Html.ValidationMessageFor(model => model.GiaoLyVienId, null, new { @class = "help-inline" })
</td>
<td>
    @Html.TextBoxFor(model => model.NamHoc)
    @Html.ValidationMessageFor(model => model.NamHoc, null, new { @class = "help-inline" })
</td>
<td>
    @Html.EditorFor(model => model.DiemTBHK1)
    @Html.ValidationMessageFor(model => model.DiemTBHK1, null, new { @class = "help-inline" })
</td>
<td>
    @Html.EditorFor(model => model.DiemTBHK2)
    @Html.ValidationMessageFor(model => model.DiemTBHK2, null, new { @class = "help-inline" })
</td>
<td>
    @Html.EditorFor(model => model.DiemTongKetNamHoc)
    @Html.ValidationMessageFor(model => model.DiemTongKetNamHoc, null, new { @class = "help-inline" })
</td>
<td>
    @Html.DropDownList("LopId", String.Empty)
    @Html.ValidationMessageFor(model => model.LopId, null, new { @class = "help-inline" })
</td></tr>

但是我点击Add Details后的结果只是添加到表格的标题上方而不是右栏中。

http://i808.photobucket.com/albums/zz1/nquangkhaiDST/Capture_zpsabd9817f.png

我不明白为什么。知道怎么解决吗?非常感谢。

2 个答案:

答案 0 :(得分:3)

问题出在桌面上的div。

尝试按以下步骤操作

@Ajax.ActionLink(
    "Thêm Chi Tiết", 
    "AddDetails", 
    new AjaxOptions { 
        HttpMethod = "GET", 
        OnSuccess = "successCall" 
    }
)

然后是successCall函数,你可以在表中添加新行

<script type="text/javascript">
     function successCall(result) {
             $("#tableId").append(result);
     }
</script>

答案 1 :(得分:1)

试试这个,

@Ajax.ActionLink(
    "Thêm Chi Tiết", 
    "AddDetails", 
    new AjaxOptions { 
        HttpMethod = "GET", 
        OnSuccess = "successCall" 
    }
)

<强>功能

<script type="text/javascript">
     function successCall(result) {
             $("#tableId").append(result.Data);
     }
</script>

成功后,您可以使用append(result.Data)绑定表格中的数据。