RenderPartial和ViewBag [英] RenderPartial and ViewBag
问题描述
我正在尝试修改以下代码: http://www.codeproject.com/Articles/260470/PDF-reportingusing-ASP-NET-MVC3 要获取ViewBag数据以在PDF中呈现视图.
I'm trying to modify this code: http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3 To make it to get the ViewBag data for render the View in the PDF.
基本上,我需要将ViewBag的新上下文传递给Html.RenderPartial,现在的代码是:
Basically, I need to pass to Html.RenderPartial with a new Context my ViewBag, the code now is:
Html.RenderPartial(viewName, model);
我正在尝试将其更改为:
And I'm trying to change it to something like:
Html.RenderPartial(viewName, model, new ViewDataDictionary(ViewBag));
我没有收到任何错误,但字典空着.
I don't get any error but the Dictionary keeps empty.
BR
推荐答案
尝试传递ViewData
属性而不是ViewBag
.
Html.RenderPartial(viewName, model, new ViewDataDictionary(ViewData));
ViewBag
属性是一个动态对象,因此您正在调用的ViewDataDictionary
构造函数是采用object
的构造函数.您最好使用已经填充了ViewDataDictionary
的代码. ViewBag和ViewData中的数据相同.
The ViewBag
property is a dynamic object, so the ViewDataDictionary
constructor you are calling is the one that takes a object
. You're better off to use the one that takes an already populated ViewDataDictionary
. The data in the ViewBag and ViewData are the same.
这篇关于RenderPartial和ViewBag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!