Ajax调用在asp.mvc中不起作用

时间:2012-03-04 12:26:49

标签: ajax asp.net-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元素中..但是空白页面上显示的时间(意味着有回发而不是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>

2 个答案:

答案 0 :(得分: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脚本。那些已经过时了。

答案 1 :(得分:0)

您似乎还没有包含您不引人注意的Jquery文件。

将此添加到您的页面:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>
相关问题