Pages

Tuesday, May 24, 2011

Passing quick data to Views with ViewBag

The new ViewBag is a dynamic wrapper around ViewData that allows passing the data from controller to the view. One of the issues at the moment is that ViewBag contents can't be passed to HTML Helpers as parameters directly.

The workaround for this is to explicitely cast the ViewBag property to the expected type.

So, instead of

@Html.TextArea("customXml", ViewBag.CustomMessage, new { @style = "width: 100%; height: 400px; " });

you'd have to write

@Html.TextArea("customXml", (string) ViewBag.CustomMessage, new { @style = "width: 100%; height: 400px; " });

No comments: