Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"> @ViewBag.myData <input type="hidden" name="anotherexample" value="anotherexamplevalue" /> </form> 

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 <>

Thanks Paul

like image 237
Sunrise Avatar asked Oct 13 '11 15:10

Sunrise


People also ask

Can we use ViewBag in Blazor?

can we make use of viewdata or viewbag or tempdata in blazor server application? Thanks for contacting us. Blazor does not postback hence having TempData or ViewData does not apply to Blazor applications. We have directed your feedback to the appropriate engineering team for further evaluation.

What is ViewBag in Cshtml?

ASP.NET MVC - ViewBag. The ViewBag in ASP.NET MVC is used to transfer temporary data (which is not included in the model) from the controller to the view. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class.

What is razor view in ASP NET MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What is razor view in asp net core?

A view is an HTML template with embedded Razor markup. Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup.


1 Answers

Use @Html.Raw(ViewBag.Mydata).

like image 188
Yngve B-Nilsen Avatar answered Sep 22 '22 22:09

Yngve B-Nilsen