如果标题被切断,我收到以下错误: 0x800a01b6 - JavaScript运行时错误:对象不支持属性或方法'datepicker'
datepicker在作为PartialView返回的Modal表单中工作正常,但是当它尝试在Modal / PartialView外部的完整View中使用它时会抛出该错误。
Bundle.Config:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-2.2.0.js",
"~/Scripts/bootstrap.js",
"~/Scripts/bootstrap-datepicker.js")
);
bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
"~/Scripts/jquery-ui-1.11.4.js",
"~/Scripts/bootstrap-datepicker.js")
);
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*",
"~/Scripts/jquery.unobtrusive*"
));
bundles.Add(new StyleBundle("~/bundles/css").Include(
"~/Content/bootstrap.css",
"~/Content/bootstrap-theme*",
"~/Content/jquery-ui.css",
"~/Content/jquery-ui.structre.css",
"~/Content/jquery-ui.theme.css"
));
bundles.Add(new StyleBundle("~/bundles/themes/base/css").Include(
"~/Content/themes/*.css"
));
bundles.Add(new StyleBundle("~/bundles/datepickers/css").Include(
"~/Content/bootstrap-datepicker.css",
"~/Content/themes/base/datepicker.css"
));
}
管理员布局(表格应在哪里):
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Boxer Puppy Dogs Admin</title>
@*<script src="~/Scripts/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />*@
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~/Content/Admin.css" rel="stylesheet" type="text/css" />
@Styles.Render("~/bundles/css")
@Styles.Render("~/bundles/themes/base/css")
@Styles.Render("~/bundles/datepickers/css")
@RenderSection("styles", required: false)
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@RenderSection("scripts", required: false)
主要布局(为了保持相对一致)
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Boxer Puppy Dogs</title>
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
@*<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />*@
@Styles.Render("~/bundles/css")
@Styles.Render("~/bundles/themes/base/css")
@Styles.Render("~/bundles/datepickers/css")
@RenderSection("styles", required: false)
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@RenderSection("scripts", required: false)
模态部分视图:
@model BoxerPuppyDogs.Models.ManageLitters.LitterViewModel
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>
@Html.BeginForm("SaveNewLitter", "ManageLitter", FormMethod.Post, new { @id = "newLitterForm" })
{
<div class="modal fade" id="NewLitterDialog" tabindex="-1" role="dialog" aria-labelledby="RemoveModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Litter</h4>
</div>
<div class="modal-body">
<div id="DialogPlaceHolder">
@Html.Partial("_NewLitterFormFields", Model)
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
}
完整视图版本:
@model BoxerPuppyDogs.Models.ManageLitters.LitterViewModel
@{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
ViewBag.Title = "Add Litter";
}
<script src="~/Scripts/jquery-ui-1.11.4.min.js"></script>
<div class="container">
<div class="row">
@Html.BeginForm("SaveNewLitter", "ManageLitter", FormMethod.Post, new { @id = "newLitterForm" })
{
@Html.Partial("_NewLitterFormFields", Model)
</div>
<div class="row">
<button type="button" class="btn btn-default">Cancel</button>
</div>
}
</div>
Modal和Full视图都调用此Partial:
@model BoxerPuppyDogs.Models.ManageLitters.LitterViewModel
<script type="text/javascript">
$(document).ready(function () {
$("#LitterDOB").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'mm/dd/yy',
gotoCurrent: true
});
});
</script>
<div class="row">
@Html.LabelFor(m => m.LitterDOB)
@Html.TextBoxFor(m => m.LitterDOB, String.Format("{0:MM/dd/yyyy}", Model.LitterDOB), new { id = "LitterDOB", @class = "datepicker" })
</div>
我最初尝试使用EditorFor并为此创建自定义模板,但这被证明是令人讨厌的,我只需要在一个控件上使用它,所以此时并不是真的有必要。
调用Partial的Modal和View之间必须有一些区别,但是我看不到它。
答案 0 :(得分:0)
问题是我有
@RenderSection("scripts", required: false)
在布局的头部而不是在“身体”部分的底部。