Sunrise Sunrise - 1 year ago 92
HTML Question

How do I render HTML from the Viewbag using MVC3 Razor

I am trying to pass a form element into an MVC3 view by using the Viewbag and simply write the HTML to the page ...

In controller:

ViewBag.myData = "<input type=""hidden"" name=""example"" value=""examplevalue"">";

In view (I know I could use a helper for the form):

<form action="http://exampleurl/examplepage" method="post" id="example-form">
<input type="hidden" name="anotherexample" value="anotherexamplevalue" />

This renders the myData as text in the HTML i.e.:

&lt;type="hidden" name="example" value="examplevalue"&gt;

So my question is how do I get Razor to do the equivalent of older:

<%= myData %>

and not replace the <>


Answer Source

Use @Html.Raw(ViewBag.Mydata).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download