为什么 ViewBag 数据为空?
Why is the ViewBag data empty?
我正在尝试使用 MVC 和 iText 从我的网页创建一个 pdf,但是当我到达视图时,ViewBag
数据为空。
在 Controller 中,ViewBag 仍然有值。
为什么ViewBag数据为空?
在视图中启动:
<td><span>
<a href='<%: Url.Action("HitlijstNaarPdf", new { AID_Hitlijst = ViewBag.Huidig })%>' target="_blank">
Download lijst
</a></span>
</td>
在控制器中调用结果
public FileStreamResult HitlijstNaarPdf(int? AID_Hitlijst = 0)
{
SelectPeriode(AID_Hitlijst);
GetHitlijstSettings();
var model = new HitlijstModel();
string sHtmlInhoud= this.RenderView("Index", model);
FileStreamResult result = PDFGenerator(sHtmlInhoud);
return result;
}
private void GetHitlijstSettings()
{
ViewBag.Datum = FHitlijstSettings.Datum;
ViewBag.Jaar = FHitlijstSettings.Jaar;
ViewBag.Week = FHitlijstSettings.Week;
}
在 class ControllerExtensions 中:
public static class ControllerExtensions
{
public static string RenderView(this Controller controller, string viewName, object model)
{
return RenderView(controller, viewName, new ViewDataDictionary(model));
}
public static string RenderView(this Controller controller, string viewName, ViewDataDictionary viewData)
{
var controllerContext = controller.ControllerContext;
var viewResult = ViewEngines.Engines.FindView(controllerContext, viewName, null);
StringWriter stringWriter;
using (stringWriter = new StringWriter())
{
var viewContext = new ViewContext(
controllerContext,
viewResult.View,
viewData,
controllerContext.Controller.TempData,
stringWriter);
// Here viewResult.ViewBag.Week still has data
// by debugging you see:
// viewResult.ViewBag.Week = 14;
// viewResult.ViewBag.Datum = "maandag 6 april 1987";
viewResult.View.Render(viewContext, stringWriter);
viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
}
return stringWriter.ToString();
}
}
而当系统returns到View时(ViewBag.Week <=> null):
<th class="hitlijst_datum">Hitlijst - nummer <%: ViewBag.Week %> - <%: ViewBag.Datum %></th>
您收到以下消息:
以下方法或属性之间的调用不明确:'System.IO.TextWriter.Write(string, params object[])' 和 'System.IO.TextWriter.Write(char[])'
我试过:Html.Raw 和 ToString => Html.Raw(ViewBag.Week.ToString()) 也一样,但这也不起作用。我必须做什么才能确保 ViewBag 在 View 中不为空?
答案如下:http://weblog.west-wind.com/posts/2012/May/30/Rendering-ASPNET-MVC-Views-to-String。我用这个来源替换了 ControllerExtensions。它有效。如果有人能告诉我,为什么通过 ControllerExtensions class 的 ViewBag 消失了。高兴听到。
我正在尝试使用 MVC 和 iText 从我的网页创建一个 pdf,但是当我到达视图时,ViewBag
数据为空。
在 Controller 中,ViewBag 仍然有值。
为什么ViewBag数据为空?
在视图中启动:
<td><span>
<a href='<%: Url.Action("HitlijstNaarPdf", new { AID_Hitlijst = ViewBag.Huidig })%>' target="_blank">
Download lijst
</a></span>
</td>
在控制器中调用结果
public FileStreamResult HitlijstNaarPdf(int? AID_Hitlijst = 0)
{
SelectPeriode(AID_Hitlijst);
GetHitlijstSettings();
var model = new HitlijstModel();
string sHtmlInhoud= this.RenderView("Index", model);
FileStreamResult result = PDFGenerator(sHtmlInhoud);
return result;
}
private void GetHitlijstSettings()
{
ViewBag.Datum = FHitlijstSettings.Datum;
ViewBag.Jaar = FHitlijstSettings.Jaar;
ViewBag.Week = FHitlijstSettings.Week;
}
在 class ControllerExtensions 中:
public static class ControllerExtensions
{
public static string RenderView(this Controller controller, string viewName, object model)
{
return RenderView(controller, viewName, new ViewDataDictionary(model));
}
public static string RenderView(this Controller controller, string viewName, ViewDataDictionary viewData)
{
var controllerContext = controller.ControllerContext;
var viewResult = ViewEngines.Engines.FindView(controllerContext, viewName, null);
StringWriter stringWriter;
using (stringWriter = new StringWriter())
{
var viewContext = new ViewContext(
controllerContext,
viewResult.View,
viewData,
controllerContext.Controller.TempData,
stringWriter);
// Here viewResult.ViewBag.Week still has data
// by debugging you see:
// viewResult.ViewBag.Week = 14;
// viewResult.ViewBag.Datum = "maandag 6 april 1987";
viewResult.View.Render(viewContext, stringWriter);
viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
}
return stringWriter.ToString();
}
}
而当系统returns到View时(ViewBag.Week <=> null):
<th class="hitlijst_datum">Hitlijst - nummer <%: ViewBag.Week %> - <%: ViewBag.Datum %></th>
您收到以下消息:
以下方法或属性之间的调用不明确:'System.IO.TextWriter.Write(string, params object[])' 和 'System.IO.TextWriter.Write(char[])'
我试过:Html.Raw 和 ToString => Html.Raw(ViewBag.Week.ToString()) 也一样,但这也不起作用。我必须做什么才能确保 ViewBag 在 View 中不为空?
答案如下:http://weblog.west-wind.com/posts/2012/May/30/Rendering-ASPNET-MVC-Views-to-String。我用这个来源替换了 ControllerExtensions。它有效。如果有人能告诉我,为什么通过 ControllerExtensions class 的 ViewBag 消失了。高兴听到。