简体   繁体   English

当我尝试加载部分视图时,为什么要加载新页面?

[英]Why is a new page loaded when I try to load a partial view?

I've written an application in ASP.NET MVC5. 我已经在ASP.NET MVC5中编写了一个应用程序。 On one of the pages, I want the user to be able to select a book title from a drop-down list and then have another drop-down list generate when they have chosen one. 在其中一个页面上,我希望用户能够从下拉列表中选择一个书名,然后在选择一个书名时生成另一个下拉列表。 The second list will contain chapters that correspond to the book that was selected. 第二个列表将包含与所选书相对应的章节。 The problem is that, after the book has been selected, a new blank webpage is generated with just the new drop-down list in it. 问题在于,选择书籍后,会生成一个新的空白网页,其中只包含新的下拉列表。

Here is the CSHTML code that is intended to generate the two drop-down lists. 这是旨在生成两个下拉列表的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>
}

And here is the code for the second drop-down list in "_ChapterDropDown.cshtml". 这是“ _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>

And finally, here is the C# code to open the second drop-down list. 最后,这是打开第二个下拉列表的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);
}

I can't understand why this code would generate a brand new page. 我不明白为什么这段代码会产生一个全新的页面。 Why does it not add the new drop-down list to the existing view? 为什么不将新的下拉列表添加到现有视图? Any help on this topic would be greatly appreciated. 任何有关此主题的帮助将不胜感激。 And by the way, I have set "UnobtrusiveJavaScriptEnabled" to true in "Web.config" and have added the following lines of code to the head of "_Layout.cshtml". 顺便说一句,我在“ 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>

In all the cases I've encoutered where @Ajax functionality was displaying a partial view on a new page the problem was always that the jquery.unobtrusive library was not correctly referenced. 在所有我遇到的情况下, @Ajax功能都在新页面上显示部分视图,问题始终是未正确引用jquery.unobtrusive库。

To confirm that this is the problem is very simple: 确认这是问题很简单:

1)Temporarily remove the Layout page: 1)暂时删除“布局”页面:

@{
    Layout = null;
}

2)At the top of your child view add these CDN references: 2)在子视图的顶部,添加以下CDN引用:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.ajax.unobtrusive/3.2.4/jquery.unobtrusive-ajax.min.js"></script>

If you confirm that it's working after making the changes above you can bring back the code for referencing the master page, remove the two js references and fix your script references in the master page.Follow these rules: 如果在完成上述更改后确认它可以正常工作,则可以带回引用母版页的代码,删除两个js引用并在母版页中修复脚本引用。请遵循以下规则:

  1. Firstly you should add a reference to jQuery, right at the top of the master page. 首先,您应该在母版页顶部添加对jQuery的引用。
  2. Add the reference to the jquery.unobtrusive library AFTER jQuery. 将引用添加到jQuery 之后jquery.unobtrusive库。
  3. You should only have one reference to the jquery.unobtrusive file.Don't have a minified and a standard version as references, you only need one. 您应该只对jquery.unobtrusive文件有一个引用。没有jquery.unobtrusive化版本和标准版本作为引用,您只需要一个。
  4. Do not re-add the javascript references to the child views.You either reference the scripts in the master\\layout page or in the child views, not both. 不要将javascript引用重新添加到子视图中。您可以在master \\ layout页面或子视图中引用脚本,而不要同时引用两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM