【问题标题】:Html.RenderPartial Works but Html.RenderAction not working (is blank) and no errors or exceptionsHtml.RenderPartial 工作,但 Html.RenderAction 不工作(为空白)并且没有错误或异常
【发布时间】:2013-11-29 04:25:28
【问题描述】:

这是我正在尝试的

布局

<!DOCTYPE html>
<html lang="en">
 <head>
  <title>WebApp Name</title>
  <meta charset="utf-8" />
 </head>
 <body>
  <div id="menuContainer">
   @{Html.RenderPartial("~/Views/CustomPartials/_MainMenuPartial.cshtml");} @*Works*@
   @*@{Html.RenderAction("GetMenuStructure", "Custom");}*@ @*Not working*@
  </div>
  <div>
   @RenderBody()
  </div>
 </body>
</html>

控制器和动作方法

namespace Webappl.Controllers
{
 [Authorize]
 public class CustomController : Controller
 {
   [AllowAnonymous]
   [ChildActionOnly]
   public ActionResult GetMenuStructure()
   {
    return PartialView("~/Views/CustomPartials/_MainMenuPartial.cshtml");
   }
 }
}

_MainMenuPartial.cshtml

<ul id="ul_MainMenuStructure">
    <li><a href="#">Guest Pg1</a></li>
    <li><a href="#">Guest Pg2</a></li>
    <li><a href="#">Guest Pg3</a></li>
</ul>

问题是

@{Html.RenderPartial("~/Views/CustomPartials/_MainMenuPartial.cshtml");}

将_MainMenuPartial.cshtml的内容完美渲染到布局中。

虽然

@{Html.RenderAction("GetMenuStructure", "Custom");}

什么都不做。没有错误。没有例外。只是空白。如果我在

处设置断点
return PartialView("~/Views/CustomPartials/_MainMenuPartial.cshtml");

断点被击中。我按 F5 并继续前进,没有任何错误/异常,但菜单结构不会在布局中呈现。我只有一片空白

<div id="menuContainer">
</div>

当我在浏览器中查看时,在最终呈现的页面中。

我需要通过 action 方法来执行某些自定义逻辑。这就是为什么渲染部分是不够的。

我在包括 stackoverflow 在内的任何地方都找不到类似的行为。这可能是糟糕的 binging/谷歌搜索技能。

我们将非常感谢您的帮助。让我头疼了 3 天了。

【问题讨论】:

  • 谦虚地请求一些帮助。我确信 StackOverflow 中有专家可以提供帮助。我尽力发挥我的新手能力,但无法找出为什么会发生这种情况。有人请帮忙!!!
  • 尊敬的管理员我想知道我的问题是否对其他人可见。因为它与“Stackoverflow”非常不同,甚至没有对问题的单一回应。我看到许多其他更简单和更复杂的问题至少在发布后的几个小时内得到了评论。 10 天,甚至没有一个回应让我想知道。这也是一个非常相关的问题......
  • 我遇到了完全相同的问题。是你自己想出来的吗?

标签: asp.net-mvc-4 renderaction html.renderpartial


【解决方案1】:

我只是改变了

@Html.RenderAction("X", "Y")

@Html.Action("X", "Y")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2018-02-25
    • 2015-08-24
    • 2014-09-30
    相关资源
    最近更新 更多