2

私はすでにここでいくつかの投稿を見てきましたが、私は賢明ではありません. 私は Ajax の最初の例を試しましたが、役に立ちませんでした。部分ビューは、現在のページの 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 に答える