Questions tagged [razor]
Razor is a template language used by ASP.NET Web Pages, ASP.NET MVC (since version 3), and ASP.NET Core. It adds a layer of abstraction above HTML generation. It supports seamless transitions between HTML markup and C# or VB code. Transitions between markup and code are indicated by the "@" sign.
razor
33,306
questions
786
votes
12
answers
331k
views
How do I import a namespace in Razor View Page?
How to import a namespace in Razor View Page?
682
votes
16
answers
311k
views
Escape @ character in razor view engine
I am creating a sample ASP.NET MVC 3 site using Razor as view engine. The razor syntax starts with @ character e.g. @RenderBody(). If I write @test on my cshtml page it gives me parse error
CS0103: ...
485
votes
7
answers
245k
views
How to use ternary operator in razor (specifically on HTML attributes)?
With the WebForms view engine, I'll commonly use the ternary operator for very simple conditionals, especially within HTML attributes. For example:
<a class="<%=User.Identity.IsAuthenticated ? "...
470
votes
13
answers
447k
views
Using Razor within JavaScript
Is it possible or is there a workaround to use Razor syntax within JavaScript that is in a view (cshtml)?
I am trying to add markers to a Google map... For example, I tried this, but I'm getting a ...
461
votes
7
answers
309k
views
Writing/outputting HTML strings unescaped
I've got safe/sanitized HTML saved in a DB table.
How can I have this HTML content written out in a Razor view?
It always escapes characters like < and ampersands to &.
430
votes
7
answers
463k
views
How to declare a local variable in Razor?
I am developing a web application in asp.net mvc 3.
I am very new to it. In a view using razor, I'd like to declare some local variables and use it across the entire page. How can this be done?
It ...
403
votes
10
answers
510k
views
How to get current page URL in MVC 3
I am using the Facebook comments plugin on a blog I am building. It has some FBXML tags that are interpreted by the facebook javascript that is referenced on the page.
This all works fine, but I have ...
394
votes
8
answers
187k
views
Styles.Render in MVC4
In a .NET MVC4 project how does @Styles.Render works?
I mean, in @Styles.Render("~/Content/css") which file is it calling?
I dont have a file or a folder called "css" inside my Content folder.
384
votes
23
answers
251k
views
Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine
I have this section defined in my _Layout.cshtml
@RenderSection("Scripts", false)
I can easily use it from a view:
@section Scripts {
@*Stuff comes here*@
}
What I'm struggling with is how ...
350
votes
6
answers
592k
views
HTML.ActionLink vs Url.Action in ASP.NET Razor
Is there any difference between HTML.ActionLink vs Url.Action or they are just two ways of doing the same thing?
When should I prefer one over the other?
342
votes
6
answers
108k
views
ASP.NET MVC View Engine Comparison
I've been searching on SO & Google for a breakdown of the various View Engines available for ASP.NET MVC, but haven't found much more than simple high-level descriptions of what a view engine is.
...
337
votes
3
answers
186k
views
How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?
I would like to have 2 separate Layouts in my application. Let's say one is for the Public section of the website and the other is for the Member side.
For simplicity, let's say all the logic for each ...
309
votes
5
answers
71k
views
ASP.NET MVC 3 - Partial vs Display Template vs Editor Template
So, the title should speak for itself.
To create re-usable components in ASP.NET MVC, we have 3 options (could be others i haven't mentioned):
Partial View:
@Html.Partial(Model.Foo, "SomePartial")
...
298
votes
26
answers
437k
views
How to get the Display Name Attribute of an Enum member via MVC Razor code?
I've got a property in my model called Promotion that its type is a flag enum called UserPromotion. Members of my enum have display attributes set as follows:
[Flags]
public enum UserPromotion
{
...
282
votes
8
answers
191k
views
Replace line break characters with <br /> in ASP.NET MVC Razor view
I have a textarea control that accepts input. I am trying to later render that text to a view by simply using:
@Model.CommentText
This is properly encoding any values. However, I want to replace ...