简体   繁体   English

使用Ajax Beginform加载不同的局部视图

[英]Loading different partial view with Ajax Beginform

I just created a small test project to see if it's possible to load a different partial view on an ASP.NET MVC Razor page, but it seems like I failed to do so. 我刚刚创建了一个小型测试项目,看看是否可以在ASP.NET MVC Razor页面上加载不同的局部视图,但似乎我没有这样做。 Instead of replacing the "partial" div by "Partial", it opens "Partial" as a new page... 它不是用“部分”替换“部分”div,而是打开“Partial”作为新页面......

Can anyone tell me what I'm doing wrong? 谁能告诉我我做错了什么?

Current situation: 现在的情况:

Overview/Index.cshtml: 概述/ Index.cshtml:

@model dynamic

@{
    ViewBag.Title = "title";
}

<h2>title</h2>

@Html.Partial("AnotherPartial")

Overview/AnotherPartial.cshtml: 概述/ AnotherPartial.cshtml:

@model dynamic

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

@{
    ViewBag.Title = "title";
}

    <h2>title</h2>
    @using(Ajax.BeginForm("Post", "Overview", new AjaxOptions{InsertionMode = InsertionMode.Replace, UpdateTargetId = "partial", HttpMethod = "POST"}))
    {
        <button type="submit">Submit</button>
    }
<div id="partial">
</div>

Report/Partial.cshtml: 报告/ Partial.cshtml:

@model dynamic

@{
    ViewBag.Title = "title";
}

<h2>Partial</h2>

OverviewController: OverviewController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AjaxWebTest.Controllers
{
    public class OverviewController : Controller
    {
        // GET: Overview
        public ActionResult AnotherPartial()
        {
            return PartialView();
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public PartialViewResult Post()
        {
            return PartialView("~/Views/Report/Partial.cshtml");
        }
    }
}

ReportController: ReportController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AjaxWebTest.Controllers
{
    public class ReportController : Controller
    {
        // GET: Report
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Partial()
        {
            return PartialView();
        }
    }
}

EDIT: Found the solution: 编辑:找到解决方案:

Found the problem, jQuery wasn't defined on my page, so the unobtrusive-ajax js file didn't load properly. 发现问题,我的页面上没有定义jQuery,所以unobtrusive-ajax js文件没有正确加载。 Added following reference BEFORE my unobtrusive-ajax include: 在我不引人注意的ajax之前添加了以下参考:

<script src="~/Scripts/jquery-1.10.2.js"></script>

You should put your unobtrusive-ajax script reference in the Scripts section of your main view. 您应该在主视图的“ Scripts部分中放置不显眼的ajax脚本引用。

@model dynamic

@{
    ViewBag.Title = "title";
}

<h2>Main Index page title</h2>

@Html.Partial("AnotherPartial")

@section Scripts
{
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
}

In the Layout file, @RenderSection("scripts", required: false) is called at the very bottom after loading jQuery library. 在布局文件中,在加载jQuery库之后,在最底层调用@RenderSection("scripts", required: false) The unobtrusive-ajax script is expecting jQuery to be available/already loaded. unobtrusive-ajax脚本期望jQuery可用/已经加载。

<div id="pageContent">
    @RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)

So if you want to execute some js files from your other views, you need to include those in the scripts section so that when the page is fully rendered, it will be added to the bottom ( After other dependeny libraries like jQuery is loaded); 因此,如果您想从其他视图执行某些js文件,则需要在脚本部分中包含这些文件,这样当页面完全呈现时,它将被添加到底部(在加载其他依赖库(如jQuery)之后);

Found the problem, jQuery wasn't defined on my page, so the unobtrusive-ajax js file didn't load properly. 发现问题,我的页面上没有定义jQuery,所以unobtrusive-ajax js文件没有正确加载。 Added following reference BEFORE my unobtrusive-ajax include: 在我不引人注意的ajax之前添加了以下参考:

<script src="~/Scripts/jquery-1.10.2.js"></script>

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

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