2012-12-14 58 views
0

我已经看过几篇文章已经在这里,但我不明智。我尝试了我的第一个Ajax例子,但无济于事。部分视图加载到单独的页面,而不是更改当前页面中的dom元素。ASP.NET MVC 3 - 部分视图displayling作为新页面

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

请如果有一个这么容易做到这一点,你可以让我知道吗?

查看:

@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> 

Conrtoller:

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

回答

2

你有没有包含/引用的所有必要的JavaScript文件?

如果您UnobtrusiveJavaScriptEnabled那么你需要:

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

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

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

这些文件可以当你创建一个新的MVC3项目都可以找到。

+0

查看源所示: 那么我错过了什么? – Glen

+0

是否有任何javascript错误?这些文件是否真的存在于Scripts文件夹中?尝试使用JavaScript文件的非最小版本。我曾在MVC 3项目 – heads5150

+0

中看到它们的损坏版本无JavaScript错误。是的,他们存在。我没有改变任何标准的ASP.NET MVC3项目。我已经添加了你提到的脚本。不知道有关提到的第一个。 (URL,“jquery-unobtrusive-ajax.min.js”) @ Content脚本(URL,“jquery.validate.js”) @ Content.Script(Url,“ jquery.validate.unobtrusive.js“) } – Glen

相关问题