我已经在这里看过一些帖子,但我并不聪明。我尝试了我的第一个 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);
}