【问题标题】:why ajax.actionlink not refresh the page?为什么ajax.actionlink 不刷新页面?
【发布时间】:2012-03-08 15:41:36
【问题描述】:

首先,对不起我的英语不好。 我不明白为什么当我点击删除用户时我的页面没有刷新...... 点击后我签入数据库,用户被删除,但我的表格页面没有刷新,我不明白。

我的看法是:

@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);
        }
    }

我不明白为什么它不起作用,谢谢你的帮助!

【问题讨论】:

  • 行了!谢谢你的帮助

标签: ajax asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 razor


【解决方案1】:

UpdateTargetId = "deleteUser" 表示用id="deleteUser" 刷新一个DOM 元素。你没有这样的元素。

你有:

<div class="deleteUser">

不一样的是:

<div id="deleteUser">

所以用 id 替换类,你的表应该会正常刷新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 2020-11-21
    • 2023-03-20
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多