2

我已经在这里看过一些帖子,但我并不聪明。我尝试了我的第一个 Ajax 示例,但无济于事。Partial View 被加载到一个单独的页面中,而不是更改当前页面中的 dom 元素。

目的是通过单击“刷新”链接仅更新当前行的“上次活动日期”值。

请问如果有一个简单的方法可以做到这一点,你也可以告诉我吗?

看法:

@model IEnumerable<RegistrationManager.User>

@{
    ViewBag.Title = "Users";
}

@section scripts {
    @Content.Script(Url, "jquery-unobtrusive-ajax.min.js")
}
<h2>Users</h2>


<table>
    <tr>
        <th>Email Address</th>
        <th>Given Names</th>
        <th>Surname</th>
        <th>Last Activity Date</th>
        <th>Refresh</th>
    </tr>

@foreach (var item in Model)
{
    string selectedRow = "";
    if (ViewBag.UserId != null && item.UserId == ViewBag.UserId)  
    {  
        selectedRow = "selectedrow";  
    }  
    <tr class="@selectedRow" valign="top">
       <td>
            @item.UserName
        </td>
        <td>
            @item.Profile.GivenNames
        </td>
        <td>
            @item.Profile.Surname
        </td>
        <td>
            <div id="@String.Format("LastActivityDate{0}", item.UserId)">@Html.Partial("_DateOnlyPartialView", item.LastActivityDate)</div>
        </td>
        <td>
            @Ajax.ActionLink("Refresh", "Refresh",
                         new { UserId = item.UserId },
                         new AjaxOptions {
                             UpdateTargetId = "LastActivityDate" + item.UserId,
                             InsertionMode = InsertionMode.Replace,
                             HttpMethod = "GET"                                 
                        })
        </td>
    </tr>
}

</table>

控制器:

public PartialViewResult Refresh(Guid? UserId)
{
    User user = _db.Users.Find(UserId);
    user.RefreshLastActivityDate();
    return PartialView("_DateOnlyPartialView", user.LastActivityDate);
 }
4

1 回答 1

2

您是否包含/引用了所有必要的 javascript 文件?

如果你有 UnobtrusiveJavaScriptEnabled 那么你需要:

  1. jQuery
  2. jquery.unobtrusive-ajax.js

如果您还使用客户端验证,则需要;

  1. jquery.validate.js
  2. jquery.validate.unobtrusive.js

这些文件都可以在您创建新的 MVC3 项目时找到。

于 2012-12-14T01:56:57.183 回答