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.
33,273
questions
786
votes
12
answers
330k
views
How do I import a namespace in Razor View Page?
How to import a namespace in Razor View Page?
677
votes
16
answers
308k
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: ...
483
votes
7
answers
243k
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
445k
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 &.
429
votes
7
answers
460k
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
508k
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 ...
395
votes
8
answers
186k
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
249k
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 ...
348
votes
6
answers
588k
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.
...
335
votes
3
answers
185k
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
70k
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")
...
294
votes
26
answers
430k
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
189k
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 ...
267
votes
11
answers
84k
views
Preprocessor directives in Razor
I am writing my first Razor page today, and can't figure out how to enter
#if debug
...
#else
...
#endif
How can I do that in Razor?
262
votes
6
answers
127k
views
ASP.NET MVC Razor render without encoding
Razor encodes string by default. Is there any special syntax for rendering without encoding?
260
votes
9
answers
151k
views
Serving favicon.ico in ASP.NET MVC
What is the final/best recommendation for how to serve favicon.ico in ASP.NET MVC?
I am currently doing the following:
Adding an entry to the very beginning of my RegisterRoutes method:
routes....
259
votes
3
answers
130k
views
@media media query and ASP.NET MVC razor syntax clash
I've got a large site that runs in ASP.NET MVC using the Razor view engine.
I have a base stylesheet which contains all of the generic styling for the whole site. On occasion, however, I have page ...
254
votes
8
answers
129k
views
How do I define a method in Razor?
How do I define a method in Razor?
253
votes
6
answers
238k
views
What is the @Html.DisplayFor syntax for?
I understand that in Razor, @Html does a bunch of neat things, like generate HTML for links, inputs, etc.
But I don't get the DisplayFor function...
Why would I write:
@Html.DisplayFor(model => ...
250
votes
6
answers
173k
views
How to create a function in a cshtml template?
I need to create a function that is only necessary inside one cshtml file. You can think of my situation as ASP.NET page methods, which are min web services implemented in a page, because they're ...
248
votes
27
answers
231k
views
Razor View throwing "The name 'model' does not exist in the current context"
After significant refactoring in my MVC 4 application, and Razor shows this error while debugging Views:
The name 'model' does not exist in the current context.
This is the offending line of code:
...
238
votes
2
answers
179k
views
ASP.NET MVC 3 Razor: Include JavaScript file in the head tag
I'm trying to figure out the proper Razor syntax to get a JavaScript file for a particular *.cshtml to be in the head tag along with all the other include files that are defined in _Layout.cshtml.
224
votes
6
answers
323k
views
How to reference a .css file on a razor view?
I know how to set .css files on the _Layout.cshtml file, but what about applying a stylesheet on a per-view basis?
My thinking here is that, in _Layout.cshtml, you have <head> tags to work with,...
209
votes
7
answers
138k
views
Where and how is the _ViewStart.cshtml layout file linked?
Here's the About.cshtml from the default MVC 3 template:
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
Put content here.
</p>
I would expect that a reference ...
206
votes
5
answers
204k
views
How to add a second css class with a conditional value in razor MVC 4
While Microsoft has created some automagic rendering of html attributes in razor MVC4, it took me quite some time to find out how to render a second css class on an element, based on a conditional ...
203
votes
8
answers
221k
views
Mix Razor and Javascript code
I'm pretty confused with how to mix razor and js. This is the current function I am stuck with:
<script type="text/javascript">
var data = [];
@foreach (var r in Model.rows)
...
203
votes
2
answers
206k
views
How to write a comment in a Razor view?
How to write a comment in a MVC view, that won't be transmitted to the final HTML (i.e.,to browser, to response). One can make a comment with:
<!--<a href="/">My comment</a> -->
...
202
votes
16
answers
227k
views
ASP.NET MVC 3 Razor - Adding class to EditorFor
I'm trying to add a class to an input.
This is not working:
@Html.EditorFor(x => x.Created, new { @class = "date" })
202
votes
24
answers
182k
views
MVC 4 @Scripts "does not exist"
I have just created an ASP.NET MVC 4 project and used Visual Studio 2012 RC to create a Controller and Razor Views for Index and Create Actions.
When I came to run the application, and browsed to the ...
189
votes
7
answers
141k
views
Razor View Engine : An expression tree may not contain a dynamic operation
I have a model similar to this:
public class SampleModel
{
public Product Product { get; set; }
}
And in my controller I get an exception trying to print out
@Html.TextBoxFor(p => p....
189
votes
2
answers
140k
views
How can I add a class attribute to an HTML element generated by MVC's HTML Helpers?
ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink(), @Html.BeginForm() and so on.
I know I can specify form attributes by creating an anonymous object and pass ...
188
votes
5
answers
152k
views
Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor
Why by default were these changed when adding a new "edit" view? What are advantages when using EditorFor() vs. TextboxFor()?
I found this
By default, the Create and Edit scaffolds now use ...
187
votes
10
answers
255k
views
Add CSS or JavaScript files to layout head from views or partial views
Layout pages head:
<head>
<link href="@Url.Content("~/Content/themes/base/Site.css")"
rel="stylesheet" type="text/css" />
</head>
A View (AnotherView) from the ...
186
votes
4
answers
357k
views
How can I pass parameters to a partial view in mvc 4
I have a link like this:
<a href='Member/MemberHome/Profile/Id'><span>Profile</span></a>
and when I click on this it will call this partial page:
@{
switch ((string)...
183
votes
7
answers
105k
views
Using Razor, how do I render a Boolean to a JavaScript variable?
How do I render a Boolean to a JavaScript variable in a cshtml file?
Presently this shows a syntax error:
<script type="text/javascript" >
var myViewModel = {
isFollowing: @Model....
180
votes
4
answers
130k
views
How to use ? : if statements with Razor and inline code blocks
I'm updating my old .aspx views with the new Razore view engine. I have a bunch of places where I have code like this:
<span class="vote-up<%= puzzle.UserVote == VoteType.Up ? "-selected" : "" %...
168
votes
8
answers
124k
views
Razor HtmlHelper Extensions (or other namespaces for views) Not Found
I don't know if this was happening in the PR or Beta, but if I create an extension method on HtmlHelper, it is not recognized in a Razor powered page:
namespace SomeNamespace.Extensions {
public ...
162
votes
3
answers
89k
views
How do I write unencoded Json to my View using Razor?
I'm trying to write an object as JSON to my Asp.Net MVC View using Razor, like so:
<script type="text/javascript">
var potentialAttendees = @Json.Encode(Model.PotentialAttendees);
</script&...
161
votes
12
answers
64k
views
Dynamic Anonymous type in Razor causes RuntimeBinderException
I'm getting the following error:
'object' does not contain a definition for 'RatingName'
When you look at the anonymous dynamic type, it clearly does have RatingName.
I realize I can do this with ...
159
votes
1
answer
60k
views
Correct way to use _viewstart.cshtml and partial Razor views?
I'm using _viewstart.cshtml to automagically assign the same Razor Layout to my views.
It's a dead simple file in the root of my Views folder that looks like this:
@{
Layout = "~/Views/Shared/...
158
votes
10
answers
30k
views
Disable Visual Studio code formatting in Razor
This has been asked before: Why doesn't Visual Studio code formatting work properly for Razor markup?
But that question is a couple years old. And Razor formatting is still completely ...
156
votes
7
answers
164k
views
MVC 3: How to render a view without its layout page when loaded via ajax?
I am learning about Progressive Enhancement and I have a question about AJAXifying views. In my MVC 3 project I have a layout page, a viewstart page, and two plain views.
The viewstart page is in the ...
156
votes
6
answers
34k
views
Why does Razor _layout.cshtml have a leading underscore in file name?
In the default ASP.NET MVC 3 project, layout & partial cshtml files start with an underscore
_viewstart
_Layout
_LogOnPartial
Why this convention, and what is this used for? Do I need to follow ...
150
votes
13
answers
578k
views
DropDownList in MVC 4 with Razor
I'm trying to create a DropDownList on a razor view.
Would someone help me with this?
Normal HTML5 code:
<select id="dropdowntipo">
<option value="Exemplo1">Exemplo1</option>
...
148
votes
5
answers
50k
views
How to add extra namespaces to Razor pages instead of @using declaration?
Is there some sort of magic I need to use to get the namespaces in the pages/namespaces element in the webconfig?
<pages>
<namespaces>
<add namespace="System.Web.Mvc"/>
...
145
votes
11
answers
155k
views
MVC which submit button has been pressed
I have two buttons on my MVC form:
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
From my Controller ...
139
votes
17
answers
52k
views
Visual Studio 2015 Broken Razor Intellisense
After installing and then repairing my VS2015 instance I still cannot get intellisense (server side) to work in my MVC views.
I get alerted by message prompt as soon as I open for the first time in a ...
139
votes
6
answers
70k
views
MVC3 Razor: Displaying html within code blocks
In my cshtml files I have a lot of blocks with stuff like this:
@if(Model.foo)
{
<span>Hello World</span>
}
The only reason the span is there is because I can't find any other way to ...