当我启动Home / index.cshtml(在Visual Studio 2013中按F5)时,网站崩溃时出现此错误:
0x800a138f - JavaScript运行时错误:无法获取未定义或空引用的属性“值”
但是,当我启动网站Shared / _Layout.cshtml时,它可以正常工作。
主/ index.cshtml:
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<p id="greet-message" class="lead"></p>
</div>
<!-- Scripts -->
<script type="text/javascript">
$(function () {
// declare a reference to loginhub
console.log("Logging in ...");
// ***!!!*** HERE IT CRASHES! ***!!!***
var login = $.connection.loginHub;
//get's called when the user have been authenticated i.e. logged in
login.client.greet = function (message) {
$("#greet-message").html(message);
};
//start the hub
$.connection.hub.start().done(function () {
login.server.greet();
});
});
</script>
共享/ _Layout.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")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="signalr/hubs"></script>
</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("Home", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
</div>
@RenderSection("scripts", required: false)
</body>
</html>
_ViewStart.cshtml
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
默认情况下,Shared / _Layout.cshtml加载Home / index.cshtml。
我认为可能是因为文档没有准备好所以我将index.cshtml中的第一个函数更改为使用$( document ).ready(function()
,但没有区别。
我使用了jQuery 1.10,但在这篇文章之后我把它降级为1.8.2,但没有区别:jquery validation error in Asp.net MVC - unable to get value of property 'call' : object is null or undefined
我正在使用jquery.signalR-2.2.0
,如果这可能会导致一些问题。
从index.cshtml而不是_Layout.cshtml启动网站有什么区别?