Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor - Outputting text in-position to the screen

I create a String in a view and want to output it to the screen.

Initially I tried Response.Write but, due to reasons explained elsewhere on this site, the content appeared at the top of the page. I then attempted to output the string using @ like so: @myString.

This worked, in that it output the text at the right location but it escaped HTML links that I had put in there. How do I get around this problem?

like image 472
Chris Avatar asked Jul 25 '12 15:07

Chris


People also ask

What is the difference between Razor view and Razor page?

The difference between them is that View Pages are Razor views that are used to provide the HTML representations (aka views) for services in much the same way View Pages work for MVC Controllers.

Which is better MVC or Razor?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

How do you declare a string variable in razor view?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

Which ASP.NET MVC object generates the HTML output that displays in web browser?

Razor is a powerful templating implementation that's very useful to generate text and HTML output for all sorts of purposes. As you've seen in this article, you can use ASP.NET MVC Razor views from just about anywhere in your ASP.NET applications fairly easily with the Helper class provided here.


1 Answers

You can use Html.Raw():

@Html.Raw(MyStringVar)
like image 136
Ricardo Souza Avatar answered Oct 11 '22 18:10

Ricardo Souza