RenderPartial and ViewBag

谁都会走 提交于 2019-12-10 19:01:36

问题


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.

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


回答1:


Try passing in the ViewData property instead of the ViewBag.

Html.RenderPartial(viewName, model, new ViewDataDictionary(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.




回答2:


You can use:

@Html.Partial(viewName)

In partial viewbag from main view will be accessible.

Ok. If you need render html from partial view to pdf you can use this code:

@{
    var htmlString = Html.Partial(viewName);    
    var pdfData = PdfComposerClass.CreatePdfStaticMethod(htmlString);   
}


来源:https://stackoverflow.com/questions/17300950/renderpartial-and-viewbag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!