$ reference未定义

时间:2015-05-23 00:21:55

标签: javascript jquery asp.net asp.net-mvc

我知道这意味着没有加载jquery,但我的代码似乎是正确的。

我的_layouts.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("Property", "Property", "Home")</li>
                    <li>@Html.ActionLink("App Properties", "GetAzureAadApp", "ActiveDirectory")</li>
                    <li>@Html.ActionLink("Create App Properties", "CreateProperty", "ActiveDirectory")</li>
                    <li>@Html.ActionLink("Get Extended Properties", "GetProperties", "ActiveDirectory")</li> 
                    <li>@Html.ActionLink("CreateUser", "CreateUser", "ActiveDirectory")</li>     
                    <li>@Html.ActionLink("TestRestCall", "TestRestCall", "ActiveDirectory")</li>                   
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

我的观点

@model PruebasAD.Models.SuccessViewModel
@{
    ViewBag.Title = "TestRestCall";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>TestRestCall</h2>
<article>
  <aside class="green">
    @Model.Message
  </aside>
  <aside>
    <pre id="json-result">
    </pre>
  </aside>
</article>


<script type="text/javascript">
  $(document).ready(function(){
    var str = JSON.stringify(@(new MvcHtmlString(Model.JSON)), undefined, 2); // indentation level = 2
    $('#json-result').html(str);
    console.log(str);
  });
</script>

和我的控制器动作

public async Task<ActionResult> TestRestCall()
{
    Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);
    var token = await GetAppTokenAsync();
    string requestUrl = "https://graph.windows.net/mysaasapp.onmicrosoft.com/users?api-version=2013-04-05";
    HttpClient hc = new HttpClient();
    hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
    HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
    if (hrm.IsSuccessStatusCode)
    {
        string jsonresult = await hrm.Content.ReadAsStringAsync();
        return View("TestRestCall", new SuccessViewModel
        {
            Name = "The Title",
            Message = "The message",
            JSON = jsonresult.ToJson()
        });
    }
    else
    {
        return View();
    }
}

3 个答案:

答案 0 :(得分:3)

您无需将@Scripts声明移至<head>,因为@CupawnTae建议。

相反,您可以将视图中的脚本移动到脚本部分(在布局中配置)。

@model PruebasAD.Models.SuccessViewModel
@{
    ViewBag.Title = "TestRestCall";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>TestRestCall</h2>
<article>
  <aside class="green">
    @Model.Message
  </aside>
  <aside>
    <pre id="json-result">
    </pre>
  </aside>
</article>

@section scripts{
<script type="text/javascript">
  $(document).ready(function(){
    var str = JSON.stringify(@(new MvcHtmlString(Model.JSON)), undefined, 2); // indentation level = 2
    $('#json-result').html(str);
    console.log(str);
  });
</script>
}

在你的布局中,由于你在jQuery引用下面渲染你的脚本部分,它们将以正确的顺序加载。

这一点很重要的原因是它会在页面的主要HTML之后加载脚本。该页面感觉加载速度更快,因为用户可以更快地看到该网站。这适用于具有较小HTML文档和CSS引用的站点。

但是,您可能也对Where is the best place to put tags in HTML markup?感兴趣,后者讨论了有关放置标记的更好做法。

答案 1 :(得分:2)

尝试移动

@Scripts.Render("~/bundles/jquery")

进入<head>部分,以便在您尝试访问它之前加载它。 Adding a defer attribute将允许页面继续加载而无需等待jQuery完成加载(on supported browsers)。

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.RenderFormat("<script src='{0}' defer='defer'></script>","~/bundles/jquery")
</head>

答案 2 :(得分:0)

您的脚本在JQuery之上运行。在下面添加一个脚本部分,在模板文件中加载JQuery。然后在视图中填写具有页面特定脚本的部分。这会将您的脚本放在JQuery下面,并将脚本放在底部以帮助处理页面加载时间