2012-04-20 69 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