无法在 ASP.NET MVC3 中的部分视图中访问 ViewBag
我有一个调用视图的控制器。在视图中有一个名为 @Html.Partial("ViewName", model) 的
这工作正常。PartialView
。
但是在控制器中,我希望将一些东西放入视图袋中,而这些东西很难放入我传递给视图的视图模型中。 主视图访问 ViewBag
没有问题,但在 PartialView
中它不返回任何内容。
在这种情况下是否可以使用 ViewBag,或者我应该将这些数据“破解”到传递给视图的模型中(以及传递给 PartialView 的模型),以及我传递给嵌套在第一个 PartialView
中的 PartialView
的模型)?
I have a controller calling a view. In the view there is a PartialView
called be @Html.Partial("ViewName", model).
This works fine.
But in the controller
I wish to put something in the viewbag what would be hard to put in the viewmodel I pass to the view.
The main view have no problem accessing the ViewBag
, but in the PartialView
it does not return anything.
Is it possible to use the ViewBag
in this case or should I "hack" this data into the model I pass to the view (and the model I pass to the PartialView
, and the model I pass to the PartialView
nested in the first PartialView
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这应该没有任何问题。在我的 HomeController Index 操作中,我向 ViewBag 添加一条消息:
在索引视图上,我添加部分视图:
在部分视图上,我渲染消息:
从下面的评论中:当您将模型传递给局部视图。然后你可以参考原来的ViewBag
That should work without any problems. In my HomeController Index action I add a message to the ViewBag:
On the Index View I add the partial view:
And on the partial view I render the message:
From the comments below: there seems to be a problem when you pass a model to the partial view. Then you can refer to the original ViewBag with
如果您使用
Html.Partial()
的重载,其中viewData
是输入参数之一,例如:那么您的部分视图将看不到原始数据查看包。
删除
new ViewDataDictionary(ViewBag)
,所以你应该写If you are using an overload of the
Html.Partial()
whereviewData
is one of the input parameters, for example:then your partial view will not see data from your original ViewBag.
Remove
new ViewDataDictionary(ViewBag)
, so you should write在评论 TehOne 中说:
这对我有用。
In a comment TehOne said:
Which is what worked for me.
最近我遇到了同样的问题,但这里的答案只是解决方法,因为他们建议不要使用
viewData
使用Html.Partial
的重载。但是,如果您必须使用这些重载,我相信正确的答案是:我认为如果您需要传递不同的
ViewDataDictionary
(例如,我用它来更改HtmlFieldPrefix
),则应该使用这个答案>) 并且因为在视图中您应该知道ViewBag
中需要哪些部分视图,因此将 ViewBag 中的所有参数命名为复制到新的ViewBag
中应该不会有问题代码> 用于部分视图(显然ViewBag
使用来自ViewData
的值)。Recently I was experiencing same problem, but the answers here were just workarounds as they propose not to use the overload of
Html.Partial
usingviewData
. But if you had to use these overloads I believe the correct answer is:I think this answer should be used if you need to pass different
ViewDataDictionary
(e.g. I us it for changingHtmlFieldPrefix
) and because in the view you should know, what partial view wil need inViewBag
, so it should not be problem to name all the parameters from ViewBag to be copied into newViewBag
used in partial view (apparentlyViewBag
uses values fromViewData
).为parentView发送的模型可以在partialViewName中使用。此外,为parentView发送的ViewData也可以用于partialViewName。
您无法访问为parentView发送的 Model。
因此,从现在开始,partialViewName 中活动的 Model 就是 newModel。
为parentView发送的viewData也可用于partialViewName。
为parentView发送的模型也可以用于partialViewName
I。
这里,发送给parentView的ViewData被“new ViewDataDictionary”覆盖。
这里,如果有一个针对parentView的ViewBag,如果你像上面那样编写代码,你就无法到达它。
II.
此用法与第一个 (I.) 相同。
III.
通过此代码块,您可以到达在partialViewName中为parentView发送的ViewData和ViewBag
The Model that is sent for parentView, can be used for in the partialViewName. Moreover, the ViewData which is send for parentView can also be used for partialViewName.
You cannot reach the Model which was sent for parentView.
Therefore, from now on the Model which is active in the partialViewName is the newModel.
The viewData which is send for parentView can be used also for partialViewName.
The Model which is send for parentView can be used also for partialViewName
I.
Here, the ViewData which was sent for parentView overwrite by 'new ViewDataDictionary'.
Here, If there is a ViewBag which is for parentView, you cannot reach that if you write the code like above one.
II.
This usage is same as first one (I.).
III.
With this code block, You can reach the ViewData and ViewBag which are sent for parentView in the partialViewName
我在一个非常基本的页面上遇到了同样的问题:
并且部分视图仅包含一些对 ViewBag 的引用的文本,以获取从控制器传递的一些动态值。事实证明,部分视图的名称很重要。删除部分页面并使用“添加”->“重新创建”查看-> MVC 5 View 但命名页面 _AppIntro.cshtml 修复了它。
I was having this same problem with a very basic page:
And the partial view contained only text with some references to ViewBag to fetch some dynamic values passed from the controller. It turned out that the name of the partial view mattered. Removing the partial page and recreating it with Add -> View -> MVC 5 View but naming the page _AppIntro.cshtml fixed it.
我的情况是,我对模型
IEnumerable有了部分视图
但是我必须通过使用,因为 Model.SampleModelList 可以为
null
简单的解决方案是,因为 PartialView 现在也是 View 的一部分,View 上的每个元素都可以通过 PartialView 访问,所以我确实将 ViewBag 数据设置为View html 上的数据元素,而不是 ViewBag。
My senario was, I had partial view with Model
IEnumerable<SampleModel>
However I had to pass using, coz Model.SampleModelList can be
null
The simple solution was since PartialView is also now part of View every element on View can be accessed by PartialView so I did set the ViewBag data to data-element on View html, instead of ViewBag.
就我而言,
@ViewContext.Controller.ViewBag.Message
效果不佳。我有一个部分视图,它是从具有不同模型的不同控制器引用的,并继承自基本模型类。在这种情况下,
Controller.ViewBag
为 null。我的解决方案是:
In my case
@ViewContext.Controller.ViewBag.Message
didn't work as well.I have a partial view that is referenced from different controllers with different models, inherited from a base model class. In this case the
Controller.ViewBag
comes as null.My solution is: