2012-03-04 71 views
0

我已经在我的控制器中获得了此方法。Ajax调用不能在asp.mvc中工作

public string GetTime(string zone) 
    { 
     DateTime time = DateTime.UtcNow.AddHours(offsets[zone]); 
     return string.Format("<div>The time in {0} is {1:h:MM:ss tt}</div>", zone.ToUpper(), time); 
    } 

    private Dictionary<string, int> offsets = new Dictionary<string, int> { { "utc", 0 }, { "bst", 1 }, { "mdt", -6 }}; 

这是我的html:

@{ 
    ViewBag.Title = "Statistics"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<html> 
<head runat="server"> 
    <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" 
    type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" 
    type="text/javascript"></script> 
</head> 
<body> 
    <h2> 
     Statistics</h2> 
    <h2> 
     What time is it?</h2> 
    <p> 
     Show me the time in:<br /> 
     @Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })<br /> 
     @Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" }) 
     <br /> 
     @Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" }) 
     <br /> 
    </p> 
    <div id="myResults" style="border: 2px dotted red; padding: .5em;"> 
     Results will appear here 
    </div> 
    <p> 
     This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC) 
    </p> 
</body> 
</html> 

的问题是,当时犯规出现在div element..but呈现一个空白页面上的时间(意思是有一个回传,而不是一个ajax呼叫)。为什么会发生?

这是怎么链接的HTML加载的样子:

<script src="../../Scripts/jquery-1.5.1-vsdoc.js" type="text/javascript"></script> 

<script src="/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script> 

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script> 

<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script> 
+0

可能会尝试使用'ActionResult'作为返回类型,并将值作为'return Content(“Whatever string”);'获取。 – Oybek 2012-03-04 12:36:05

+0

我不认为这是相关的所有 – BlackFire27 2012-03-04 12:44:28

回答

1

你已经包括了布局,但在你看来,你有一个完整的<html>文件,我想,你最终在年底一些很破的HTML。

下面是你的观点可能看起来像,如果你不想使用布局:你需要

@{ 
    ViewBag.Title = "Statistics"; 
    // Explicitly specify that we don't use any layout because 
    // this view already contains the entire html document 
    Layout = null; 
} 

<html> 
<head> 
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script> 
</head> 
<body> 
    <h2> 
     Statistics</h2> 
    <h2> 
     What time is it?</h2> 
    <p> 
     Show me the time in:<br /> 
     @Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })<br /> 
     @Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" }) 
     <br /> 
     @Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" }) 
     <br /> 
    </p> 
    <div id="myResults" style="border: 2px dotted red; padding: .5em;"> 
     Results will appear here 
    </div> 
    <p> 
     This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC) 
    </p> 
</body> 
</html> 

唯一的脚本是jqueryjquery.unobtrusive-ajax。也不要在剃须刀中使用任何runat="server"属性。

,如果你想使用的布局:

@{ 
    ViewBag.Title = "Statistics"; 
} 

<h2> 
    Statistics</h2> 
<h2> 
    What time is it?</h2> 
<p> 
    Show me the time in:<br /> 
    @Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })<br /> 
    @Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" }) 
    <br /> 
    @Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" }) 
    <br /> 
</p> 
<div id="myResults" style="border: 2px dotted red; padding: .5em;"> 
    Results will appear here 
</div> 
<p> 
    This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC) 
</p> 

再次,不要忘记你的布局2个脚本。

而且最后一句话:按照惯例,所有的控制器动作应该返回ActionResults,所以:

public string GetTime(string zone) 
{ 
    DateTime time = DateTime.UtcNow.AddHours(offsets[zone]); 
    return Content(string.Format("<div>The time in {0} is {1:h:MM:ss tt}</div>", zone.ToUpper(), time)); 
} 

最后请务必不要在您的网页使用任何脚本Microsoft*.js。那些已经过时了。

+0

Thanks.Darin,这个论坛很幸运有你。还有一个问题。我确实有一个母版页...并且我定义了标题两次(Visual Studio告诉我)。如果我想覆盖masterpages title属性,该怎么办?那我会怎么做? – BlackFire27 2012-03-04 13:30:17

+0

@ BlackFire27,在您的布局中:' @ ViewBag.Title'并在您的视图中:'@ {ViewBag.Title =“Statistics”; }'。 – 2012-03-04 13:31:28

0

看来你没有包括您unobstrusive jQuery的文件。

添加到您的网页:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script> 
+0

嗯..没有帮助 – BlackFire27 2012-03-04 12:43:30

+0

你检查了这个文件已被加载?你的问题看起来像ajax事件处理程序没有连接到链接时的行为(这通常是什么阻止你切换到url,这是一个后备行为,如果有人有JS禁用 - btw:你没有js禁用?) – Marc 2012-03-04 13:04:40

+0

没有..它启用..我使用铬。 youtube/google work..so我的代码应该。我如何检查我的文件是否被加载?查看更新 – BlackFire27 2012-03-04 13:08:23