简体   繁体   English

将部分视图加载到div中

[英]Load partial view into div

I'm trying to load a partial view into a div from a calling view, however the partial view loads into a new page. 我正在尝试将部分视图从调用视图加载到div中,但是部分视图会加载到新页面中。 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. 所以我想我可能会遗漏一些基本的东西。

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 PartialView

<h1>Test</h1>

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. ASP.NET MVC 3 RC2中的Ajax.*帮助程序不再使用已弃用的MS AJAX而不支持jQuery,因此您的脚本包含错误。 You need to include jquery 你需要包含jquery
  • You have an ajax form without a submit button and an ajax link inside this form. 你有一个没有提交按钮的ajax表单和这个表单中的ajax链接。 This makes no sense. 这毫无意义。 Either use an ajax form or an ajax link. 使用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>

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之后。

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/jquery")之后添加bundle @Scripts.Render("~/bundles/jqueryval") ,或者在页面视图的脚本部分添加脚本引用,如下所示:

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

    }

Good Luck. 祝好运。

你的部分需要在ajax形式内

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

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