【发布时间】:2014-12-30 07:01:21
【问题描述】:
我正在使用 Bootstrap 模式通过 MVC 5 应用程序工作。在尝试使用来自不同网页的模式访问特定的部分页面时,我遇到了一些不一致的情况。其中一种模式工作正常,另一种则根本不起作用。
(我在学习如何做到这一点时参考了Modals in MVC 5 - 所以我可能在某个地方犯了一个愚蠢的错误,但我不这么认为)。
我将从相关代码开始,然后在底部我将回顾我尝试过的内容。
modalform.js(这几乎是从上述参考对象复制/粘贴的)
$(function () {
$.ajaxSetup({ cache: false });
$("a[data-modal]").on("click", function (e) {
// hide dropdown if any
$(e.target).closest('.btn-group').children('.dropdown-toggle').dropdown('toggle');
$('#myModalContent').load(this.href, function () {
$('#myModal').modal({
backdrop: 'static',
keyboard: true
}, 'show');
bindForm(this);
});
return false;
});
});
模态部分页面(创建)
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Add New Site</h4>
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.OrganisationSelected, htmlAttributes: new { @class = "control-label col-lg-3" })
<div class="col-md-10">
@Html.DropDownListFor(model => Model.OrganisationSelected, Model.Organisations, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrganisationSelected, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SiteName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SiteName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SiteAbbr, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SiteAbbr, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SiteAbbr, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SiteNotes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SiteNotes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SiteNotes, "", new { @class = "text-danger" })
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">Cancel</button>
<input class="btn btn-primary" type="submit" value="Save" />
</div>
</div>
</div>
}
AllSites.cshtml(这个工作正常 - 是的,我知道 for 循环不是最佳实践。)
@model IEnumerable<DRPlanner.ViewModels.OrganisationSiteViewModel>
@using DRPlanner.Helpers
@{
ViewBag.Title = "All Sites";
}
<!-- modal placeholder-->
<div id='myModal' class='modal fade in'>
<div class="modal-dialog">
<div class="modal-content">
<div id='myModalContent'></div>
</div>
</div>
</div>
@for (int i = 0; i < Model.Count(); i++)
{
if ((i == 0) || (Model.ElementAt(i).OrgId != Model.ElementAt(i - 1).OrgId))
{
<strong>
@Html.RouteLink(Model.ElementAt(i).OrganisationName.ToString() + " (" + Model.ElementAt(i).OrganisationAbbr.ToString() + ")", "OrgShort", new { org = Model.ElementAt(i).OrgId })
</strong>
@:<ul class="list-unstyled">
}
if (Model.ElementAt(i).SiteName != "")
{
<li>
@Html.RouteLink(Model.ElementAt(i).SiteName.ToString(), "SiteShort", new { org = Model.ElementAt(i).OrgId, site = Model.ElementAt(i).SiteId })
</li>
}
if ((i == Model.Count() - 1) || (!Model.ElementAt(i).OrganisationAbbr.Equals(Model.ElementAt(i + 1).OrganisationAbbr)))
{
@:</ul>
}
}
<br />
@Html.RouteLink("New Organisation", "NewOrg", new { controller = "Organisation", action = "Create" })
@Html.RouteLink("New Site", "NewSite", new { controller = "Site", action = "Create" }, new { data_modal = "", id = "btnCreate", @class = "btn btn-small btn-primary" })
Sites.cshtml(这个不行)
@model IEnumerable<DRPlanner.ViewModels.OrganisationSiteViewModel>
@{ViewBag.Title = "Sites";}
Sites For @ViewBag.OrganisationName
<ul class="list-unstyled">
@foreach (var item in Model)
{
<li>@Html.RouteLink(item.SiteName.ToString(), "SiteShort", new { site = item.SiteId })</li>
}
</ul>
@Html.ActionLink("Add Site", "Create", "Site", null, new { data_modal = "", id = "btnCreate", @class = "btn btn-small btn-primary" })
<script src="~/Scripts/modalform.js"></script>
<!-- modal placeholder-->
<div id='myModal' class='modal fade in'>
<div class="modal-dialog">
<div class="modal-content">
<div id='myModalContent'></div>
</div>
</div>
</div>
我做了什么:
我在 VS2013 调试器中调试了 modalform.js 的运行,发现当它从 AllSites.cshtml 运行时,$('#myModal').modal 指的是一个函数(我不知道这个函数是如何或从哪里拉出来的,抱歉),但是当它在 Site.cshtml 上运行时,$('#myModal').modal 未定义。所以我知道这就是错误所在:我对如何解决这个问题一无所知(或者一般来说可能只是一无所知)。另一条信息:Sites.cshtml 和 AllSites.cshtml 都是存在 Create 操作的不同控制器的操作。基本上,我试图让模态可以从站点内的多个位置访问。任何帮助都非常感谢(我还没有 15 个代表,所以以后需要按比例投票)。
错误:0x800a01b6 - JavaScript 运行时错误:对象不支持属性或方法“模态”
【问题讨论】:
-
是否已将引导 jquery 库添加到您的项目中?这个:maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js
-
是的,该项目具有所有必要的引导 javascript 文件,并且它们被正确引用(在 _Layout.cshtml 中)
标签: javascript asp.net-mvc c#-4.0 twitter-bootstrap-3