我已经在我的控制器中获得了此方法。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>
可能会尝试使用'ActionResult'作为返回类型,并将值作为'return Content(“Whatever string”);'获取。 – Oybek 2012-03-04 12:36:05
我不认为这是相关的所有 – BlackFire27 2012-03-04 12:44:28