在ASP.NET MVC3局部视图无法访问ViewBag [英] Can't access ViewBag in a partial view in ASP.NET MVC3
问题描述
我有一个控制器调用视图。在视图中有一个 PartialView
称为为 @ Html.Partial(视图名,模型)。
这工作正常
I have a controller calling a view. In the view there is a PartialView
called be @Html.Partial("ViewName", model).
This works fine.
而在控制器
我想提出一些在viewbag这将是难以用我传递给视图的视图模型。
主要看有没有问题访问 ViewBag
,而在 PartialView
它不会返回任何东西。
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.
是否有可能在这种情况下使用 ViewBag
或者我应该黑客这个数据为我传递到视图的模型(该模型我传递给 PartialView
,我传递给模型 PartialView
嵌套在第一个 PartialView
)?
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
)?
推荐答案
这应该没有任何问题。在我的HomeController中Index操作我想补充一个信息,ViewBag:
That should work without any problems. In my HomeController Index action I add a message to the ViewBag:
ViewBag.Message = "Welcome to ASP.NET MVC!";
在索引视图我添加了局部视图:
On the Index View I add the partial view:
@Html.Partial("ViewName")
而在局部视图我呈现消息:
And on the partial view I render the message:
@ViewBag.Message
完美的作品。你还干了什么?可能是打错了?
Works perfectly. What else did you do? Might be a typing mistake?
这篇关于在ASP.NET MVC3局部视图无法访问ViewBag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!