【问题标题】:MVC5 which Helper only update main content?MVC5 哪个 Helper 只更新主要内容?
【发布时间】:2015-03-14 21:50:59
【问题描述】:

在 MVC5 中,我有一个经典的布局,左侧是菜单,右侧是一个大的主要内容区域。

菜单有 3 个按钮。他们应该用不同的视图(索引,索引2,索引3)更新主要内容。

当我尝试使用 @Url.Action() 时,整个页面都会重新加载。我只希望在@RenderBody()

中呈现视图
<button onclick="location.href='@Url.Action("Index", "Home")'">Index</button>
<button onclick="location.href='@Url.Action("Index2", "Home")'">Index2</button>
<button onclick="location.href='@Url.Action("Index3", "Home")'">Index3</button>

更新 1:

<div class="row">
    <div id="col-main" class="col-sm-12">
        <div class="well"></div>
        <div id="renderBody">
            @RenderBody()
       </div>
   </div>
</div>

【问题讨论】:

  • 如果你想只更新同一页面的一部分,你需要使用ajax。
  • 很有道理,非常感谢。我会马上解决的。我想我对这里的帮手很苛刻。我的错。
  • 我返回的 html 怎么样?如何将它放入 RenderBody()?我可以将它设置在 div 上,但有没有更好的使用 RenderBody() 的最佳实践方式?
  • 您可以查看使用 Ajax 助手 Ajax.ActionLink 虽然我更喜欢纯 jquery。不知道你所说的 _ 将它放入 RenderBody()_ 是什么意思? RenderBody() 只是主要内容的“占位符”。您需要一个容器元素(例如 div)来定义添加 ajax 调用返回的 html 的位置
  • 如果不知道完整的 html 和 css,很难说。我只是使用默认的&lt;section class="content-wrapper main-content clear-fix"&gt;@RenderBody()&lt;/section&gt;

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor asp.net-mvc-5


【解决方案1】:

你可以用这个...

 @Ajax.ActionLink("Edit", "Edit", new { id = item.ID }, new AjaxOptions { UpdateTargetId = "targetDiv", InsertionMode = InsertionMode.Replace,HttpMethod = "GET"})

您可以提供您的右侧面板 ID 作为目标区域,您可以在其中呈现您想要的视图... 我希望这对你有帮助。

【讨论】:

  • 我必须添加两件事才能工作,但非常感谢让我走上正轨。 1)在控制器中,我应该返回“PartialView”而不是“View” 2)我必须添加“@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js"
猜你喜欢
  • 2012-09-25
  • 1970-01-01
  • 2012-01-31
  • 2011-09-30
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多