为什么ajax.actionlink不刷新页面?
问题描述:
首先,对不起我的英语不好。 我不明白为什么我的网页不刷新,当我点击删除用户... 点击后,我检查数据库和用户是删除,但我的网页表不刷新,我不明白。为什么ajax.actionlink不刷新页面?
我的看法是:
@model IEnumerable<SiteWebEmpty.Models.User.UserDisplay>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js")" type="text/javascript"></script>
<h2>Display User</h2>
<div id="deleteUser">
@using (Html.BeginForm())
{
<table class="tabledisplayUser" border="1">
<tr>
<th>Name FirstName</th>
<th>UserName</th>
<th>Roles</th>
<th>Choice</th>
</tr>
<tr>
<th>@Html.Editor("name")</th>
<th>@Html.Editor("username")</th>
<th>@Html.Editor("roles")</th>
<th>@Html.Editor("choice")</th>
</tr>
@foreach (var user in Model)
{
<tr>
<td class="action nameFirstName">@Html.DisplayFor(u => user.NameFirstName)</td>
<td class="action userName">@Html.DisplayFor(u => user.UserName)</td>
@if (user.Roles.Roles.Count.Equals(0))
{
<td>Nobody Role</td>
}
else
{
<td>@Html.DropDownList("Roles", user.Roles.Roles)</td>
}
<td>@Html.ActionLink("Edit", "Edit", new { UserName = user.UserName }) | @Ajax.ActionLink("Delete", "Delete", new { UserName = user.UserName },
new AjaxOptions()
{
HttpMethod = "POST",
Confirm = "Do you want delete this user?",
UpdateTargetId = "deleteUser"
})</td>
</tr>
}
</table>
}
</div>
我的控制器:
public ActionResult DisplayUser()
{
List<UserDisplay> users=getAllUserInDB();
GetAllNameFirstNameLDAP(users);
return View(users);
}
public ActionResult Delete(String userName)
{
DeleteDB(userName);
if (!Request.IsAjaxRequest())
return RedirectToAction("DisplayUser");
else
{
List<UserDisplay> users = getAllUserInDB();
GetAllNameFirstNameLDAP(users);
return PartialView("DisplayUser",users);
}
}
我不明白为什么它不工作,感谢你的帮助!
答
UpdateTargetId = "deleteUser"
表示使用id="deleteUser"
刷新DOM元素。你没有这样的元素。
您有:
<div class="deleteUser">
这是不一样的:
<div id="deleteUser">
因此,与ID代替类,你的表应该正常刷新。
这是工作!谢谢你的帮助 – Zoners 2012-03-08 15:59:25