日期选择器不工作MVC4 [英] Datepicker not working in MVC4
问题描述
Datepicker.js
Datepicker.js
$(function () {
$('#voters_bdate').datepicker();
});
这是包含在布局视图中的文件:
These are the files included in the layout view:
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/vendor/jquery.hashchange.min.js" type="text/javascript"></script>
我还包括在视图这些文件:
I also included these files in the view:
<link href="@Url.Content("~/Content/themes/base/minified/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
<script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
<script src="~/Scripts/Datepicker.js"></script>
但仍然没有工作。
but still not working.
<input type="text" name="voters_bdate" id="voters_bdate">
当我看到控制台:未定义是不是一个函数未捕获类型错误
我得到这个错误也。
I got this error also "Uncaught TypeError: undefined is not a function" when I look at the console.
推荐答案
所以,这里的默认文件_layout由MVC4模板创建,删除了所有额外的标记:
So, here's the default _Layout file created by the MVC4 template, with all the extra markup removed:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
通知所在RenderBody()是在关系,比方说,RenderSection(脚本)?记下它,因为我们回来,在一个时刻。
Notice where the RenderBody() is in relation to, say, RenderSection("scripts")? Make a note of it, because we're coming back to that in a moment.
下面是我们可以创建一个视图的一个实例:
Here's an example of a view we might create:
@{
ViewBag.Title = "Example";
}
<div>
<p>
As Gregor Samsa awoke one morning from uneasy dreams, he found himself transformed in his bed into a gigantic insect-like creature.
</p>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
当此特定操作被调用时,该视图被取代的(或注射,如果preFER)如RenderBody在_layout发生。如果视图包含一个 @section
,然后在该节得到任何代入的_layout相当于一节。鉴于_layout并查看上述情况,我们会得到这样生成的HTML:
When this particular action is called, the view gets substituted (or injected, if you prefer) where the RenderBody occurs in _Layout. If the view contains a @section
, then anything in that section gets substituted into the equivalent section in the _Layout. Given the _Layout and view above, we'd get this generated HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Example - My ASP.NET MVC Application</title>
<link href="~/Content/Site.css" rel="stylesheet" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<div>
<p>
As Gregor Samsa awoke one morning from uneasy dreams, he found himself transformed in his bed into a gigantic insect-like creature.
</p>
</div>
<script src="/Scripts/jquery-1.8.2.min.js"></script>
<script src="/Scripts/jquery.validate.min.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
现在,如果我们不直接在视图中使用一节,并参考,比方说,jQuery验证会怎么样?
Now, what happens if we don't use a section, and reference, say, jQuery Validation in the view directly?
@{
ViewBag.Title = "Example";
}
<div>
<p>
As Gregor Samsa awoke one morning from uneasy dreams, he found himself transformed in his bed into a gigantic insect-like creature.
</p>
</div>
@Scripts.Render("~/bundles/jqueryval")
然后,我们呈现的HTML如下:
Our rendered HTML then looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Example - My ASP.NET MVC Application</title>
<link href="~/Content/Site.css" rel="stylesheet" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<div>
<p>
As Gregor Samsa awoke one morning from uneasy dreams, he found himself transformed in his bed into a gigantic insect-like creature.
</p>
</div>
<script src="/Scripts/jquery.validate.min.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="/Scripts/jquery-1.8.2.min.js"></script>
</body>
</html>
,不幸的是,是一个问题。 jQuery的验证需要jQuery的,所以将无法加载,你会看到在JavaScript控制台中的错误。
That, unfortunately, is a problem. jQuery Validate requires jQuery, so it will fail to load and you will see errors in the JavaScript console.
这个概念同样适用于你可能在你的项目中使用的库,所以花一些时间学习如何使用的部分并适当捆绑。音乐商店教程是开始,如果你正在学习ASP.NET MVC一个像样的地方:的http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-1
This same concept applies to any libraries you may use in your project, so spend some time learning how to use sections and bundling appropriately. The Music Store tutorial is a decent place to start if you're learning ASP.NET MVC: http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-1
这篇关于日期选择器不工作MVC4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!