页面顺序已更改,现在getContext为空

时间:2016-09-23 17:50:37

标签: asp.net canvas razor

我更改了我的应用程序,因此在运行时会加载另一个页面,但现在我收到错误

  

无法获取未定义或空引用的属性'getContext'

它还没有造成任何问题,但我希望在此之前解决它。

在线查看这种情况通常发生在<canvas>标记位于<script>标记之前,但我无法找到它发生的位置。

@using Application.Models;
@using Newtonsoft.Json;
@{
    User user = ViewBag.User;
}

@if (ViewBag.Loader)
{
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width" />
            <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
            <title>@ViewBag.Title - Application Title</title>
            <script id="userData" class="userData" type="application/json" data-accurate-up-to="@user.AccurateUpTo">
                @Html.Raw(JsonConvert.SerializeObject(user, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }))
            </script>

            @Styles.Render("~/Styles/lib")
            @Styles.Render("~/Styles/css")   
            @Scripts.Render("~/Scripts/lib")

        </head>
        <body>
        <!-- Navbar -->
            <div id="nav">
                <div id="smallLogoHolder">
                    <img id="smallLogo" src="~/Images/sidewaysLogo.png" />
                </div>
                <div id="navContents">
                    <div id="logoHolder">
                        <img id="logo" src="~/Images/logo.png">
                        <div id="slogan">Application Name<div id="dayOTheYear"></div></div>
                        <script>
                            setInterval(function () {
                                $("#dayOTheYear").text("(Day: " + moment().dayOfYear() + ")");
                            }, 1000 * 10);
                            $("#dayOTheYear").text("(Day: " + moment().dayOfYear() + ")");
                        </script>
                    </div>

                    <ul id="menu">
                        <a href="~/Page 1"><li class="navItem @if (ViewBag.Title == "Page 1") { @("activeNavItem"); }">Page 1</li></a>
                        <a href="~/Page 2"><li class="navItem @if (ViewBag.Title == "Page 2") { @("activeNavItem"); }">Page 2</li></a>
                        <a href="~/Page 3"><li class="navItem @if (ViewBag.Title == "Page 3") { @("activeNavItem") ; }">Page 3</li></a>
                        <a href="~/Page 4"><li class="navItem @if (ViewBag.Title == "Page 4") { @("activeNavItem") ; }">Page 4</li></a>
                    </ul>

                    <div id="userSection">
                        <div id="loginSection">
                            <div id="loginSectionPic" style="background-image: url(@("path" + User.Identity.Name.Replace("DOMAIN","").Replace("/","").Replace("\\","")))"></div>

                            <div id="loginSectionContent">
                                <span id="user">@User.Identity.Name.Replace(@"DOMAIN\", "")</span>
                                <span id="userType">
                                    @if (user.SecurityGroup.Name != "N/A")
                                    {
                                        @("(" + user.SecurityGroup.Name + ")")
                                    }
                                    else
                                    {
                                        @("(No Role)")
                                    }
                                </span>

                            <div><span id="switchUserButton">Switch User</span> | <span id="logoutButton">Logout</span></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>       

        <div class="dialog notificationDialog">
            <h2 class="dialogHeader">My Title</h2>
            <div class="notificationMeta">Sent by <span class="notificationSubmittedBy"></span><br /><span class="notificationSubmittedTime"></span></div>
            <div class="notificationText">

            </div>
            <input type="button" class="markRead" value="Mark As Read" />
            <input type="button" class="closeNotification" value="Close" />
        </div>   

        @RenderBody()  

        <div id="content">
            <span id="loadingMessage">Loading...</span> <img id="dataLoadingGif" src="~/Images/loader.gif" />

            <script>
                $(window).load(function () {
                    $.get("@Request.Url.ToString()?loader=0", function (markup) {
                        $("#loadingMessage").text("Rendering...");
                        setTimeout(function () {
                            $("#content").html(markup);
                        }, 200);

                    }).fail(function (response) {
                        console.log(response);
                        $("#loadingMessage").html("<span style='color:red'>Failed</span><br/><br/>");
                        $("#dataLoadingGif").remove();

                        $("#content").append('<iframe id="yellowScreenOfDeathHolder" style="display:block; width:100%; height: 1000px;"></iframe>');

                        $("#yellowScreenOfDeathHolder").contents().find("html").html(response.responseText);
                    })
                });
            </script>
        </div>
    </body>
</html>
}
else
{
<div id="title">
    @ViewBag.Title

    @if (ViewBag.Title == "Page 2" || ViewBag.Title == "Page 3")
    {
        <span class="areaTitle"> - All Areas</span>
    }
    else if(ViewBag.Title == "Page 1" || ViewBag.Title == "Page 4")
    {
        if (((User)ViewBag.User).Area != null)
        {
            <span class="areaTitle"> - @(((User)ViewBag.User).Area.Name)</span>
        }
        else
        {
            <span class="areaTitle"> - All Areas</span>
        }    
    }
</div>
@RenderBody()

@Scripts.Render("~/Scripts/app")

}

这是我的Layout.cshtml,此处没有任何更改,我所做的只是更改RouteConfig.cs中的某些信息以转到其他页面。我链接Layout.cshtml的原因是因为当程序运行时它会通过加载Layout.cshtml然后告诉我{j}中的.getContext为空

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Default",
    url: "{action}/{id}",
    defaults: new { controller = "Home", action = "Page 2", id = UrlParameter.Optional }

此处发生的所有更改都是Page 2曾为Page 1<canvas>上的Page 1元素是否会导致问题?

更新:

经过进一步调查后,它会在@Scripts.Render("~/Scripts/app")行返回错误,所以我想知道是否应该在其他地方调用此脚本。在哪里可以称呼它?

1 个答案:

答案 0 :(得分:0)

我想我找到了解决问题的方法。由于第1页需要@Scripts.Render("~/Scripts/app")行,因此我将其从Layout.cshtml的底部移至Page 1.cshtml,因为Page 1.cshtml不再是第一个加载。

我目前正在检查以确保这不会导致其他地方出现错误,一旦我完成了这项工作,我会将此标记为答案。