所以,我有兩個輸入,在這裏我輸入名稱和用戶的電子郵件,我要去點擊一個按鈕不能重新繪製表格的jQuery
<input type="text" name="DeleteName" style="visibility:hidden">
<input type="text" name="DeleteEmail" style="visibility:hidden">
<button class="btn btn-mini btn-primary" type="button" name="delete_btn" style="visibility:hidden" onclick="DbDelete()">Delete</button>
後,從數據庫中刪除,這是我表:
<table class="table table-bordered table-hover table-striped table-condensed" id="UserTable">
<tr>
<th>UserId</th>
<th>Name</th>
<th>Email</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@item.UserId</td>
<td>@item.Name</td>
<td>@item.Email</td>
</tr>
}
我可以輸入兩個unputs(包括姓名和電子郵件)或單一個。 這是我控制器從數據庫中刪除用戶動作:
[HttpPost]
public ActionResult Delete(User userinfo)
{
const string noResult = "Search Result Not Found";
using (var uc = new UserContext())
{
if (userinfo.Name != null)
{
var itemforDelete = uc.UserList.SingleOrDefault(o => o.Name == userinfo.Name);
CommitChanges(itemforDelete, uc, noResult);
return new JsonResult { Data = itemforDelete };
}
else
{
if (userinfo.Email != null)
{
var itemforDelete = uc.UserList.SingleOrDefault(o => o.Email == userinfo.Email);
CommitChanges(itemforDelete, uc, noResult);
return new JsonResult { Data = itemforDelete };
}
}
}
return new JsonResult();
}
這是加載表視圖控制器的方法。這個按鈕
<a class="btn btn-large btn-primary" type="button" href="home/list">Show all users</a>
被點擊時,它被稱爲:
public ActionResult List() {
List<User> users;
using (var uc = new UserContext()) {
users = uc.UserList.ToList();
}
return View(users);
}
這是我的腳本:
<script type="text/javascript">
function DbDelete() {
// Get some values from elements on the page:
var deletedName = $("input[name='DeleteName']").val(),
deletedEmail = $("input[name='DeleteEmail']").val();
var user = { Name: deletedName, Email: deletedEmail };
$.ajax(
{
type: "POST",
url: "Delete",
data: JSON.stringify({ userinfo: user }),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: OnDeleteSuccess,
error: OnDeleteError
});
}
function OnDeleteSuccess(data)
{
alert("success");
//$('#UserTable').remove(data);
$("#UserTable").fnDraw();
}
function OnDeleteError(data) { alert("error"); }
</script>
用戶從數據庫中刪除,在成功處理程序阿勒特作品,但我的數據庫在頁面上沒有更新,只有在按F5後。
我在做什麼錯?
任何建議,@ usad? :( – TomatoLion 2014-09-23 13:06:21
仍然不知道,你可以添加代碼在哪裏啓動數據表? – ekad 2014-09-23 13:16:14
我已經添加了一個更多的方法('List')加載數據庫到視圖 – TomatoLion 2014-09-23 13:19:34