簡體   English   中英

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

[英]Ajax call doesnt work in 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元素中。但是時間顯示在空白頁上(意味着有回發而不是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>

您已經包含一個Layout,但是在您的視圖中,您有一個完整的<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腳本。 那些已經過時了。

看來您還沒有包含不引人注目的Jquery文件。

將此添加到您的頁面:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM