2012-04-20 64 views
1

我不知道爲什麼,但沒有發生Ajax事件。它只是鏈接我一個新的空白頁面,並顯示「NA」,因爲它調用下面的GETTIME()函數: 這是我在Index.cshtml的Hello World代碼:MVC3 AJAX ActionLink「Hello World」我無法啓動它

@ViewBag.Title = "Home Page" 
<h2>@ViewBag.Message</h2> 
<p>Hello there! Click Update to see the current time!</p> 
<div id="MyAjaxDiv"></div> 
@Html.ActionLink("Update", "GetTime", new {id="MyAjaxLink"}) 

這裏是HomeController.cs代碼:

namespace MvcApplication1.Controllers { 
    public class HomeController : Controller { 
     public ActionResult Index() { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 
      return View(); 
     } 
     public string GetTime() { 
      if (Request.IsAjaxRequest()) 
       return DateTime.Now.ToString("hh:mm:ss tt"); 
      return "NA"; 
     } 
    } 
} 

我跟着這個鏈接: Ajax.ActionLink not working, Response.IsAjaxRequest() is always false

然而,沒有任何區別-with-或-without-的AJAXifying代碼中分離myscripts.js文件通過後的建議。它仍然將我鏈接到帶有「NA」的空白頁面。 這是「/內容myscripts.js」在_layout.cshtml

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Content/myscripts.js")" type="text/javascript"></script> 

文件頭中聲明的代碼

$(function() { 
    $('#MyAjaxLink').click(function() { 
     $('#MyAjaxDiv').load(this.href); 
     return false; 
    }); 
}); 

請幫幫忙!

回答

0

它看起來像我的問題是你的選擇器。您的Javascript定位id =「mylink」,但鏈接名爲「MyAjaxLink」。嘗試改變Javascript來此:

$(function() { 
    $('#MyAjaxLink').click(function() { 
     $('#MyAjaxDiv').load(this.href); 
     return false; 
    }); 
}); 
+0

遺憾。我剛剛更新了它。它是從另一篇文章複製的。我實際上在我的代碼中使用了正確的。哪些是MyAjaxLink和MyAjaxDiv。此外,它似乎做同樣的事情讓我到空白頁面,無論是否有腳本。 – Tom 2012-04-20 08:02:19

+0

難題...你也有「Html.ActionLink」,你寫/意思是寫「Ajax.ActionLink」? – McGarnagle 2012-04-20 08:09:08

+0

D:OMG!是!哼。第二個問題......現在它工作,但時間只是第一次更新,然後它不再刷新......(編輯:我知道了,將它添加到GetTime()的前面... [OutputCache(NoStore = true,VaryByParam =「」,Duration = 0)] – Tom 2012-04-20 08:15:24

0

當你ajaxify代碼,請確保您有以下也包括在任意一個視圖的2個腳本或馬西德威

MicrosoftAjax.js 
    MicrosoftMvcAjax.js