我想使用ajax更新MVC中的表格。我已經使用ajax在數據庫中插入數據。我只是想在插入新行後更新表格。在ASP.NET MVC中使用AJAX刷新表格
PS。我試過搜索,但沒有幫助我,我仍然困惑。
Here is my code:
Main page View:
<div id="theTable">
@Html.Partial("_IPTable")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/Admin.js"></script>"`
Partial page View:
<table id="table">`
<tr>
<th>ID</th>
<th>Line</th>
<th>Supplier</th>
</tr>
@foreach (var item in ViewBag.IPTable)`
{
<tr>
<td>
@item.ID
</td>
<td>
@item.Line
</td>
<td>
@item.Supplier
</td>
</tr>
}
</table>enter code here
Controller view:
public ActionResult Admin()
{
// get data from database
return View();
}
public ActionResult _IPTable()
{
return PartialView();
}
用於插入新記錄的Ajax代碼:
<script>
$(document).ready(function() {
//function will be called on button click having id btnsave
$("#btnSave").click(function() {
$.ajax(
{
type: "POST", //HTTP POST Method
url: "AdminInsert", // Controller/View
data: { //Passing data
//Reading text box values using Jquery
Line: $("#txtLine").val(),
Supplier: $("#txtSupplier").val()
}
});
});
}); </script>
你的代碼在哪裏通過ajax插入一個新行? – Shyju
我的意思是客戶端代碼。 – Shyju
我把它貼在下面。 –