2

我是 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>

但是我单击添加详细信息后的结果它只是添加在表格标题上方而不是右列中。

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

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

4

2 回答 2

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>
于 2013-09-03T03:05:31.820 回答
2

尝试这个,

@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).

于 2013-09-03T05:40:38.160 回答