ajax 将局部视图加载到 div 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4557021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:56:26  来源:igfitidea点击:

Load partial view into div

ajaxasp.net-mvc

提问by user415394

I'm trying to load a partial view into a div from a calling view, however the partial view loads into a new page. There isn't much code to it and I've tried various ways from other posts, but it still loads into a new page. So I think I might be missing something fundamental.

我正在尝试将局部视图从调用视图加载到 div 中,但是局部视图加载到新页面中。它没有太多代码,我尝试了其他帖子中的各种方法,但它仍然加载到新页面中。所以我想我可能会遗漏一些基本的东西。

Controller

控制器

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
    return View();
}

public ActionResult Test()
{
    return PartialView("Test");
}

View

看法

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

@using (Ajax.BeginForm("Test", "Home", new AjaxOptions { InsertionMode=InsertionMode.Replace, UpdateTargetId = "mydiv" }))
{    
    @Ajax.ActionLink("Save", "Test", "Home", new AjaxOptions{ });
}

<div id="mydiv">
    @Html.Partial("Test")
</div>

PartialView

局部视图

<h1>Test</h1>

回答by Darin Dimitrov

Couple of remarks about your code:

关于您的代码的几点说明:

  • Ajax.*helpers in ASP.NET MVC 3 RC2 no longer use MS AJAX which has been deprecated in favor of jQuery, so your script inclusions are wrong. You need to include jquery
  • You have an ajax form without a submit button and an ajax link inside this form. This makes no sense. Either use an ajax form or an ajax link.
  • Ajax.*ASP.NET MVC 3 RC2 中的助手不再使用 MS AJAX,后者已被弃用而支持 jQuery,因此您的脚本包含错误。你需要包含 jquery
  • 您有一个没有提交按钮的 ajax 表单,该表单中有一个 ajax 链接。这没有任何意义。使用 ajax 表单或 ajax 链接。

So your code might look something among the lines:

所以你的代码可能看起来像以下几行:

<script src="@Url.Content("~/scripts/jquery-1.4.4.js")" type="text/javascript"></script>

@Ajax.ActionLink("Save", "Test", "Home", new AjaxOptions { 
    InsertionMode = InsertionMode.Replace, 
    UpdateTargetId = "mydiv"
})

<div id="mydiv">
    @Html.Partial("Test")
</div>

回答by RezaRa

Add this line to your page (master page or layout)

将此行添加到您的页面(母版页或布局)

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

just after MicrosoftMvcAjax.js line.

就在 MicrosoftMvcAjax.js 行之后。

回答by QMaster

I think you should use <input type="submit" />instead of @Ajax.Actionlink(...)

我认为你应该使用<input type="submit" />而不是@Ajax.Actionlink(...)

in MVC4 you can add bundle @Scripts.Render("~/bundles/jqueryval")just after @Scripts.Render("~/bundles/jquery")or add script reference in script section in page view as shown below:

在 MVC4 中,您可以在页面视图的脚本部分@Scripts.Render("~/bundles/jqueryval")之后添加包@Scripts.Render("~/bundles/jquery")或添加脚本引用,如下所示:

@section scripts
    {
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

    }

Good Luck.

祝你好运。

回答by RailRhoad

Your partial needs to be within the ajax form

您的部分需要在 ajax 表单中