【发布时间】:2016-08-02 10:17:52
【问题描述】:
我在 ASP.NET MVC5 中编写了一个应用程序。在其中一个页面上,我希望用户能够从下拉列表中选择书名,然后在他们选择一个时生成另一个下拉列表。第二个列表将包含与所选书籍相对应的章节。问题是,选择图书后,会生成一个新的空白网页,其中只有新的下拉列表。
这是用于生成两个下拉列表的 CSHTML 代码。
@{//Loop through the authors and find the selected one to show the details of
}
@foreach (Author nextAuthor in Model)
{
//Ignore this author if it is not the selected one
if (nextAuthor.Selected == false)
{
continue;
}
<fieldset class="EditAuthor">
<div class="EditAuthorLeft">
@{//Ajax form to select book by the selected author
}
@using (Ajax.BeginForm("SelectBook", "Library", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "chapters" }))
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => nextAuthor.BookID)
</div>
<div class="editor-field">
@Html.DropDownList("BookID", nextAuthor.BookList, "< Please select a book >", new { onchange = "$(this.form).submit();" })
@Html.ValidationMessageFor(model => nextAuthor.BookID)
</div>
}
@{//Ajax form to submit and save changes to the author
}
@using (Ajax.BeginForm("UpdateAuthor", "Library", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "authors" }))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model => nextAuthor.ID)
@Html.HiddenFor(model => nextAuthor.LibraryID)
//Chapter selection if there is a selected book
<div id="chapters">
@if (nextAuthor.BookID > 0)
{
@Html.Partial("_ChapterDropDown", nextAuthor)
}
</div>
@:</div>
<p>
<input type="submit" value="Update Author" />
</p>
}
</fieldset>
}
这是“_ChapterDropDown.cshtml”中第二个下拉列表的代码。
@Html.HiddenFor(model => model.BookID)
<div class="editor-label">
@Html.LabelFor(model => model.ChapterID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ChapterID, Model.ChapterList, "< Please select a chapter >")
@Html.ValidationMessageFor(model => model.ChapterID)
</div>
最后,这里是打开第二个下拉列表的 C# 代码。
[HttpPost]
public ActionResult SelectBook(int bookId)
{
Author author;
//Create the author VM with just the info needed for the chapter partial view
//Select the list of chapters from the database that we are connected to
author = new Author();
author.BookID = bookId;
author.ChapterList = new SelectList(db.Chapters.Where(c => c.BookID == bookId).OrderBy(c => c.Name).ToList(), "ID", "Name");
author.Selected = true;
return PartialView("_ChapterDropDown", author);
}
我不明白为什么这段代码会生成一个全新的页面。为什么它不将新的下拉列表添加到现有视图中?对此主题的任何帮助将不胜感激。顺便说一句,我在“Web.config”中将“UnobtrusiveJavaScriptEnabled”设置为true,并将以下代码行添加到“_Layout.cshtml”的头部。
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
【问题讨论】:
-
您是否在
jquery.unobtrusive-ajax.js之前也包含了jquery(并且您不应该同时拥有未缩小版和缩小版)。如果你的重定向,这意味着jquery.unobtrusive-ajax.js没有被正确加载 -
@StephenMuecke 我也在“_Layout.cshtml”的头部包含了
<script src="@Url.Content("~/Scripts/jquery-2.2.4.js")" type="text/javascript"></script>这一行。是的,它被放置在不显眼的 ajax 行之前。 -
那是
jquery的唯一副本吗?如果您在浏览器控制台中遇到任何错误怎么办? -
我的解决方案中也有
jquery-2.2.4.min.js,但我没有将它包含在头中。我在哪里可以找到浏览器控制台?通过查看网页的来源?因为如果这就是你的意思,我不会通过那个或通过正常的 Visual Studio 调试看到任何错误。 -
如果您不知道如何在浏览器中调试,您就不应该开发网站。按 F12 并查看控制台选项卡(然后阅读 this 或您使用的浏览器的类似文章。您只能拥有一份
jquery的副本,因为第二个在jquery.unobtrusive-ajax.js之后,它会将其删除. 并停止添加同一文件的未缩小和缩小版本
标签: c# asp.net view asp.net-mvc-5 partial-views