问题
I 'm a newbie of MVC4, i got a problem. When I want to add a row into a table in ajax, it doesn't seem like what i want. Here my code
<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>
Then i call ajax to load more row if need here is AddDetails file which will be 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>
But the result after i click Add Details it just add above the header of table and not in the right column.
http://i808.photobucket.com/albums/zz1/nquangkhaiDST/Capture_zpsabd9817f.png
I don't understand why. Any idea how to fix it? Thanks a lot.
回答1:
Try this,
@Ajax.ActionLink(
"Thêm Chi Tiết",
"AddDetails",
new AjaxOptions {
HttpMethod = "GET",
OnSuccess = "successCall"
}
)
Function
<script type="text/javascript">
function successCall(result) {
$("#tableId").append(result.Data);
}
</script>
On success you can bind data in your table using append(result.Data)
.
回答2:
The problem is with your div inside table.
Try doing as follows
@Ajax.ActionLink(
"Thêm Chi Tiết",
"AddDetails",
new AjaxOptions {
HttpMethod = "GET",
OnSuccess = "successCall"
}
)
Then is successCall function you can add new row in table
<script type="text/javascript">
function successCall(result) {
$("#tableId").append(result);
}
</script>
来源:https://stackoverflow.com/questions/18579333/how-to-add-a-row-to-table-by-ajax-actionlink