将 ViewData 传递给使用 Html.Action 返回的 PartialView
我想通过从操作方法返回部分视图来将其嵌入到 ASP.NET MVC 页面中。
在我的基本视图中,我会:
<%= Html.Action("MyPartialViewAction") %>
我的控制器将有一个操作方法,例如:
[ChildActionOnly]
public ActionResult MyPartialViewAction()
{
return PartialView("MyPartialView");
}
我希望返回的部分视图(MyPartialView)能够访问在基本页面的控制器操作中设置的ViewData,但这似乎不是案件。如果我通过在基本视图中使用以下内容插入部分视图,它会起作用:
<% Html.RenderPartial("MyPartialView") %>
但我不想这样做,因为我希望我的“MyPartialViewAction”执行逻辑来确定要返回哪个部分视图。
I want to embed a partial view in an ASP.NET MVC page by returning it from an action method.
In my base view, I would have:
<%= Html.Action("MyPartialViewAction") %>
My controller would have an action method like:
[ChildActionOnly]
public ActionResult MyPartialViewAction()
{
return PartialView("MyPartialView");
}
I expected the returned partial view (MyPartialView) to have access to the ViewData that was set in the base page's controller action but that doesn't appear to be the case. If I insert the partial view by using the following in my base view it works:
<% Html.RenderPartial("MyPartialView") %>
I don't want to do that though because I want my "MyPartialViewAction" to execute logic to determine WHICH partial view to return.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信它实际上创建了一个新的控制器,这意味着它创建的任何视图都将具有来自该控制器的 ViewData,而不是创建调用 Action 方法的视图的控制器。您可能想尝试:
I believe that it actually creates a new controller, meaning that any view that it creates will have the ViewData from that controller, not the controller that created the view that is invoking the Action method. You might want to try: