C# Razor Syntax Quick Reference

asp.net mvc, asp.net, code 0 comments suggest edit

I gave a presentation to another team at Microsoft yesterday on ASP.NET MVC and the Razor view engine and someone asked if there was a reference for the Razor syntax.

It turns out, there is a pretty good guide about Razor available, but it’s focused on covering the basics of web programming using Razor and inline pages and not just the Razor syntax.

So I thought it might be handy to write up a a really concise quick reference about the Razor syntax.

Syntax/Sample Razor Web Forms Equivalent (or remarks)
Code Block
@{ 
  int x = 123; 
  string y = "because.";
}
<%
  int x = 123; 
  string y = "because."; 
%>
      
Expression (Html Encoded)
<span>@model.Message</span>
<span><%: model.Message %></span>
Expression (Unencoded)
<span>
@Html.Raw(model.Message)
</span>
<span><%= model.Message %></span>
Combining Text and markup
@foreach(var item in items) {
  <span>@item.Prop</span> 
}
<% foreach(var item in items) { %>
  <span><%: item.Prop %></span>
<% } %>
Mixing code and Plain text
@if (foo) {
  <text>Plain Text</text> 
}
<% if (foo) { %> 
  Plain Text 
<% } %>
Using block
@using (Html.BeginForm()) {
  <input type="text" value="input here">
}
<% using (Html.BeginForm()) { %>
  <input type="text" value="input here">
<% } %>
Mixing code and plain text (alternate)
@if (foo) {
  @:Plain Text is @bar
}
Same as above
Email Addresses
Hi philha@example.com
Razor recognizes basic email format and is smart enough not to treat the @ as a code delimiter
Explicit Expression
<span>ISBN@(isbnNumber)</span>
In this case, we need to be explicit about the expression by using parentheses.
Escaping the @ sign
<span>In Razor, you use the 
@@foo to display the value 
of foo</span>
@@ renders a single @ in the response.
Server side Comment
@*
This is a server side 
multiline comment 
*@
<%--
This is a server side 
multiline comment
--%>
Calling generic method
@(MyClass.MyMethod<AType>())
Use parentheses to be explicit about what the expression is.
Creating a Razor Delegate
@{
  Func<dynamic, object> b = 
   @<strong>@item</strong>;
}
@b("Bold this")
Generates a Func<T, HelperResult> that you can call from within Razor. See this blog post for more details.
Mixing expressions and text
Hello @title. @name.
Hello <%: title %>. <%: name %>.
NEW IN RAZOR v2.0/ASP.NET MVC 4
Conditional attributes
<div class="@className"></div>
When className = null
<div></div>
When className = ""
<div class=""></div>
When className = "my-class"
<div class="my-class"></div>
Conditional attributes with other literal values
<div class="@className foo bar">
</div>
When className = null
<div class="foo bar"></div>
Notice the leading space in front of foo is removed.
When className = "my-class"
<div class="my-class foo bar">
</div>
Conditional data-* attributes.

data-* attributes are always rendered.
<div data-x="@xpos"></div>
When xpos = null or ""
<div data-x=""></div>
When xpos = "42"
<div data-x="42"></div>
Boolean attributes
<input type="checkbox"
  checked="@isChecked" />
When isChecked = true
<input type="checkbox"
  checked="checked" />
When isChecked = false
<input type="checkbox" />
URL Resolution with tilde
<script src="~/myscript.js">
</
script>
When the app is at /
<script src="/myscript.js">
</
script>
When running in a virtual application named MyApp
<script src="/MyApp/myscript.js">
</
script>

Notice in the “mixing expressions and text” example that Razor is smart enough to know that the ending period is a literal text punctuation and not meant to indicate that it’s trying to call a method or property of the expression.

Let me know if there are other examples you think should be placed in this guide. I hope you find this helpful.

UPDATE 12/30/2012: I’ve added a few new examples to the table of new additions to Razor v2/ASP.NET MVC 4 syntax. Razor got a lot better in that release!

Also, if you want to know more, consider buying the Programming ASP.NET MVC 4 book. Full disclosure, I’m one of the authors, but the other three authors are way better.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

107 responses

  1. Avatar for Shiju Varghese
    Shiju Varghese January 6th, 2011

    Very useful quick reference.

  2. Avatar for Eric Berens
    Eric Berens January 6th, 2011

    With ASP.NET 4 a new code expression syntax (<%: %>) was introduced that renders output like <%= %> blocks do – but automatically HTML encodes it before doing so. (weblogs.asp.net/...)
    That being said, are all expressions HTML encoded and if so is there a syntax that outputs the expression without encoding it beforehand?

  3. Avatar for Damon Stephenson
    Damon Stephenson January 6th, 2011

    Eric: I believe @Html.Raw("text") does that now.

  4. Avatar for Yngve B. Nilsen
    Yngve B. Nilsen January 6th, 2011

    You should also include the @* *@ compiled comment syntax.. That for me is an excellent piece of Razor-magic :)

  5. Avatar for Dmitry Podrezov
    Dmitry Podrezov January 6th, 2011

    To Eric Berens
    @{ var text = "don't unescape me"; }
    @Html.Raw(text)
    Comments:
    Razor

    @* comments are marked like this *@

    ASP.NET

    <%-- comments are marked like this --%>

    Another useful example:

    @foreach (var item in collection) {
    @* something *@
    }

    is parsed as well as:
    @foreach (var item in collection)
    {
    @* something *@
    }

    meaning that you can move opening curly brace to the next line.
    In the sample "Mixing code and plain text (alternate)" it can be mentioned that you can use razor syntax inside escaped plain text:

    @if (foo) {
    @:Plain Text is @value
    }

  6. Avatar for Tomas Jansson
    Tomas Jansson January 6th, 2011

    Really nice quick reference guide!

  7. Avatar for Jonas Eriksson
    Jonas Eriksson January 6th, 2011

    I find the @functions and @helper very useful

    @functions
    {
    string SayWithFunction(string message)
    {
    return message;
    }
    }
    @helper SayWithHelper(string message)
    {

    Text: @message


    }
    @SayWithFunction("Hello, world!")
    @SayWithHelper("Hello, world!")

  8. Avatar for Nova Software
    Nova Software January 6th, 2011

    Thanks, but, when will Microsoft will publish the official documents?

  9. Avatar for Mike
    Mike January 6th, 2011

    "Notice in the last example that Razor is smart enough to know that the ending period is a literal text punctuation and not meant to indicate that it’s trying to call a method or property of the expression."
    Then teach it.

  10. Avatar for Joe
    Joe January 6th, 2011

    "Thanks, but, when will Microsoft will publish the official documents?"
    +1

  11. Avatar for terryterry's-chicken
    terryterry's-chicken January 6th, 2011

    I'm really impressed that you can use AddModelError on normal razor pages now. Well done Haack!
    One old nuGet of a question. ViewBag? I've always thought you gave ViewData the wrong name. Call it ControlData or FormData, because it does indeed store information used by input controls. Then call ViewBag, ViewData.

  12. Avatar for David Carrillo (@dacanetdev)
    David Carrillo (@dacanetdev) January 6th, 2011

    Very useful Quick Reference. Planning start new MVC project with Razor as View Engine

  13. Avatar for haacked
    haacked January 6th, 2011

    Thanks for the feedback! I've incorporated many of your suggestions. @Mike, I don't understand your comment at all. Teach it what?

  14. Avatar for James Still
    James Still January 6th, 2011

    awesome thx. the syntax is pretty straightforward I've discovered. Can you post a little something on extension helper methods? Things like Html.RadioButtonListFor? I'm still not clear on that.

  15. Avatar for Erx
    Erx January 7th, 2011

    Please include vb razor as well... It's not cool ur ignoring 30% of developers

  16. Avatar for ctrlShiftBryan
    ctrlShiftBryan January 7th, 2011

    I don't seem to have @Html.Raw(). I do have MVC 3 RC2, I think. The listed version is (3.0.11029.0) is that RC2?

  17. Avatar for Eric
    Eric January 7th, 2011

    Thanks Phil, that's a hot cubicle pinup for folks new to Razor!
    Razor is awesome indeed.

  18. Avatar for Martin
    Martin January 7th, 2011

    You have had a guide about Conditional Compilation Statements earlier, and they work fine with Razor, the color change of the condition which is not true is not working though.
    But unlike WebForms, it doesnt crash the intellisense in the editor when using them.

  19. Avatar for Bas Jansen
    Bas Jansen January 8th, 2011

    It seems that MVC 3 is RTM because of a checking in the Orchard Project (orchard.codeplex.com/.../403731d7ad50). Bit strange too find it out like this. But nevertheless congratulations!

  20. Avatar for Rodrigo Caballero
    Rodrigo Caballero January 9th, 2011

    Hi Phil,
    I'm using the Telerik extensions for ASP.NET MVC and there's the following code for the webforms engine:
    <% Html.Telerik().PanelBar()
    .Name("PanelBar")
    .Items(items =>
    {
    items.Add()
    .Content(() =>
    {
    %>
    <span>This is content inside a method</span>
    <%
    });
    })
    %>
    I can't make it run with razor sintax ( not even with parenthesis characters around the spans). Note that Webforms sintax is "closing" the code definition for a while, pastes a little html markup and then keeps working with the rest of the method's code
    How about this kind of webforms well-working functionality on razor?
    best regards
    Rodrigo
    PD. more of this case on:
    demos.telerik.com/aspnet-mvc/panelbar/templates

  21. Avatar for Andrey Taritsyn
    Andrey Taritsyn January 9th, 2011

    Hi, Rodrigo!
    Try the following code:
    @(Html.Telerik().PanelBar()
    .Name("PanelBar")
    .Items(items => items
    .Add().Content(@<span>This is content inside a method</span>)
    )
    )
    In Telerik Blogs is a good article on this subject - blogs.telerik.com/....

  22. Avatar for Developer Seer
    Developer Seer January 10th, 2011

    Phil I'm eager to ask this to someone.
    What is the performance loose for this awesomeness?
    I mean is this analyzed during runtime?
    or is it compiled in any form?
    Thanks!

  23. Avatar for arootbeer
    arootbeer February 23rd, 2011

    What is the correct way to make generic method calls in razor? I don't seem to be able to google it appropriately :)
    I want to be able to do

    <td>@MyStringMethod<MyClass>(params)</td>

  24. Avatar for haacked
    haacked February 24th, 2011

    Make it an explicit expression.
    @(MyStringMethod<MyClass>(params))

  25. Avatar for arootbeer
    arootbeer February 25th, 2011

    Ah, okay. Might I suggest updating your table with that? The example explicit expression doesn't really seem to address the point of explicit expressions AFAICT.
    Thanks for the tip, and a useful post.

  26. Avatar for haacked
    haacked February 25th, 2011

    @arootbeer done!

  27. Avatar for michael herndon
    michael herndon February 27th, 2011

    So when is this post going to be put into first Razor Syntax quick cheat sheet (pdf / exportable format version) ?

  28. Avatar for Stuart Clement
    Stuart Clement March 10th, 2011

    How can I use the <asp:calendar> control in a webmatrix cshtml page? Is this possible?

  29. Avatar for wtq
    wtq March 30th, 2011

    I don't know what the meaning of <text></text>in the mvc3

  30. Avatar for Gareth Elms
    Gareth Elms April 6th, 2011

    I should have read this last night before twanting (Twitter ranting) about Razor, I'd regard having to use @: as a gotcha. I started using Razor last night, and I've heard a lot about how Razor means you don't need to think about the code/markup separation, but it's not strictly true and I hit this gotcha immediately

    @if( Request.IsAuthenticated){
    Hello @Page.User.Identity.Name
    }else{
    @Html.ActionLink( "Log On", "LogOn", "Account")
    }

  31. Avatar for Gareth Elms
    Gareth Elms April 6th, 2011

    @wtg <text> is a fake html tag that helps Razor know to flip out of server code parsing and into markup parsing. The text tag is actually rendered but it is invalid html/xhtml. So it's not ideal. Use @: instead

  32. Avatar for haacked
    haacked April 7th, 2011

    Well yeah, there's always going to be those cases where you do have to think about it. After all, there's no way for Razor to know in that case that you meant for that to be literal text unless you tell it.
    But in most cases (at least in my experience), I start such blocks with a tag, such as a SPAN. In that case, it really does flow.

  33. Avatar for sandeep
    sandeep April 10th, 2011

    hi,
    i am working on mvc3 project using Razor view engine.
    i have to apply dynamically classes on div.
    my syntax is given below:

    @{
    View.Title = "About Us";
    }

    <h2>About</h2>
    @using(Html.BeginForm("About","HomeController")){
    int section = 0;
    if (section == 0)
    {
    <div class="student">
    }
    else
    {
    <div class="teacher">
    }
    </div>


    }

    At runtime it gives below error:
    Parser Error Message: The using block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
    how we can handle such dynamic design conditions

  34. Avatar for Meriem
    Meriem April 10th, 2011

    Hi, please i want the syntaxe in razor of the ajax toolkit calendar "<asp:calendar ...
    Please help.. Thank you!

  35. Avatar for Ethan
    Ethan April 20th, 2011

    Awesome, cooked and ready! thanks

  36. Avatar for Darshan
    Darshan May 24th, 2011

    Phill u saved my time. thank you for quick guide.

  37. Avatar for Arvin Boggs
    Arvin Boggs May 30th, 2011

    Please change the title to "C# Razor Syntax Quick Reference".

  38. Avatar for Ehsan
    Ehsan August 18th, 2011

    Very useful
    Thanks

  39. Avatar for Sharmin
    Sharmin August 23rd, 2011

    Nice small article.

  40. Avatar for Deany Web Geek
    Deany Web Geek October 26th, 2011

    Excellent, very useful :)

  41. Avatar for estetik
    estetik January 17th, 2012

    good article.
    thanks

  42. Avatar for Joe
    Joe February 4th, 2012

    Great post! This will come in handy. Thanks!

  43. Avatar for David Willington
    David Willington March 19th, 2012

    I've hit a problem with the following code

    <ul>
    @foreach (var venue in Model.venues) {
    <li id='vc_@Html.Raw(venue.code)'>@Html.Raw(venue.name)</li>
    }
    </ul>

    The output is

    <ul>
    <li id='vc_@Html.Raw(venue.code)'>Venue 1</li>
    <li id='vc_@Html.Raw(venue.code)'>Venue 2</li>
    <li id='vc_@Html.Raw(venue.code)'>Venue 3</li>
    </ul>

    I can't see how to have the @Html.Raw(venue.code) parsed. The best I've come up with so far is to try and escape the @Html.Raw(venue.code), ie \@Html.Raw(venue.code) which means the code is parsed but I get the \ in the output, ie

    <ul>
    <li id='vc_\v1'>Venue 1</li>
    <li id='vc_\v2'>Venue 2</li>
    <li id='vc_\v3'>Venue 3</li>
    </ul>

    I think this would do, but it's not what I'm after (I'd like to avoid the \ character in the output). Can anyone suggest a way round it?
    Thanks

  44. Avatar for Alan G
    Alan G April 17th, 2012

    This syntax should work:
    <li id='vc_@(Html.Raw(venue.code))'>@Html.Raw(venue.name)</li>

  45. Avatar for Alex Ilyin
    Alex Ilyin April 25th, 2012

    Thanks for post

  46. Avatar for Kevin Dietz
    Kevin Dietz May 13th, 2012

    Good resource.
    Things to add: @using to reference namespaces, @model to declare the type of the model, @RenderSection to insert a section (content placeholder), and @section to declare section used by @RenderSection, and a full quick reference for all @Html calls. Thanks.

  47. Avatar for Ramya
    Ramya May 30th, 2012

    Good one for beginners

  48. Avatar for Tomasz
    Tomasz May 31st, 2012

    I believe there's a bug in VS 2010 regarding the mixed code/plain text formatting.
    @if (true)
    {
    @:Plain text
    }
    Every time I format the code (Edit > Advanced > Format Document) there's a tab added before the closing curly brace...

  49. Avatar for Subramanyam Vijayanagaram
    Subramanyam Vijayanagaram July 10th, 2012

    Good Reference Article. Thanks for your posts

  50. Avatar for Robert
    Robert July 13th, 2012

    One omission: how to create if{ markup } else { markup }
    Nothing I try works

  51. Avatar for Saeed Neamati
    Saeed Neamati July 16th, 2012

    Great article Haacked, or should I say, great reference. I think you can upload this article to Wikipedia.
    I specifically enjoyed the delegates defined inside Razor. They are truly helpful in writing rich articles with many HTML markups inside them.

  52. Avatar for Padmanaban
    Padmanaban July 20th, 2012

    HI,
    Thank you for your razar synax.
    Regards
    Padmanaban

  53. Avatar for I am wondering how can I conve
    I am wondering how can I conve July 23rd, 2012

    <% Html.AjaxForm(s =>
    { %>
    <div class="form2">
    <br />
    <label class="smallLabel" for="Name" >
    Name : <span id="s_Name">*</span></label>
    <%=Html.TextBox("Name", ViewData.Model.Name, new { @class = "smallTextBox" })%>
    </div>
    <% });%>

  54. Avatar for Nick Gilbert
    Nick Gilbert August 7th, 2012

    How about a section for URLs?
    What's the equivalent of:
    @filename
    For me it's just printing the "~" rather than resolving it.

  55. Avatar for Amit
    Amit August 14th, 2012

    Hi,
    Nice article..Is there any article/tutorial where I can get the syntax of all the methods available at @Html in asp.net MVC razor?
    Thanks

  56. Avatar for sivva
    sivva August 29th, 2012

    html.raw() here what is th use of html.it is a predefined class or what can you explain ?and also tell me about helper classes.

  57. Avatar for jayesh goyani
    jayesh goyani September 6th, 2012

    Very useful reference.

  58. Avatar for Divya
    Divya September 24th, 2012

    Hello Phil,
    Thanks for useful reference. I am still confused with whether i should use Html Helper or Plain HTML.
    My Question
    ------------
    1) A lot of people (even novices) know how to read HTML. The HTML Helper syntax can confuse easily.
    2) In many cases you need to do more typing writing an HTML 'helper', then you need to write the actual HTML.
    3) The HTML Helper is going to spew real HTML anyway, so why not write HTML directly?
    4) Writing HTML gives you more control over the syntax, and the standards. You can make it conform to whatever HTML standard you want.
    5) Good way to use Razor syntax with Knockout.js and JQuery
    Are there any special compelling reasons that I have not understood (since I am self-educated in MVC and there could be gaps) that should make me want to prefer HTML Helpers?
    Or are they just code noise?

  59. Avatar for haacked
    haacked September 24th, 2012

    @Divvya It really depends on whether the HTML you're writing is static or includes server generated content. The HTML helpers in the later case can do some of the following:
    * Make sure attribute values are properly HTML encoded for security.
    * Render values based off of your model.
    * Override rendered values with values submitted by the user.
    etc. You're certainly free to use standard HTML, but you may find it's not so clean as you thought it would be as you start writing real applications.
    Now, there's a few cases where I think the HTML helpers are uglier. For example, Html.ActionLink is a case where maybe you're better off writing a normal anchor tag and use Url.Action within the href attribute of that anchor tag.

  60. Avatar for htxuankhoa
    htxuankhoa November 10th, 2012

    Thanks you... nice posts :)
    Regards

  61. Avatar for NR
    NR November 14th, 2012

    It is very quick reference for R&D

  62. Avatar for anil
    anil December 5th, 2012

    I don't no the Idea about Razor,
    Any one can help me the beginners for Razor lo learn best websites and Blogs,

  63. Avatar for P.N.Rao
    P.N.Rao December 11th, 2012

    A good reference indeed!

  64. Avatar for Nomail
    Nomail January 2nd, 2013

    if is were equal to isn't, this comment made sense

  65. Avatar for R. Schreurs
    R. Schreurs January 11th, 2013

    How can I render a colon after an expression? Say, I have a resource returning a label text. At some place, I want a colon immediately behind it.

    Example: resource UserName = "Name of the user"
    @Html.LabelFor(m => m.UserName, @Resources.MyResources.UserName:)
    will give me a build error:
    Only assignment, call, increment, decrement, and new object expressions can be used as a statement
    Of course, as a work around, I could add the colon to the resource's value.

  66. Avatar for haacked
    haacked January 11th, 2013

    What output are you trying to display? It's not clear from your question.

  67. Avatar for Aaron
    Aaron January 21st, 2013

    Hi Sandeep, I am having this same issue. Did you manage to figure out a resolution? Thanks

  68. Avatar for Geo News
    Geo News January 28th, 2013

    Generate Span tags in Viewfile like this

    @Html.DisplayFor(model => model.Reading, "Reading")

  69. Avatar for Irsathameen
    Irsathameen February 5th, 2013

    NIce

  70. Avatar for Lelala
    Lelala February 14th, 2013

    We are still unclear regarding the performance of razor - until today we've not seen any valid numbers of real live installs in comparison to webform installs. Any ideas where to look up?

  71. Avatar for Ahammad Fekri
    Ahammad Fekri March 1st, 2013

    Nice Post ... Helped Me ....
    Ahammad Fekri :)

  72. Avatar for Great programmer
    Great programmer March 24th, 2013

    Hello!!! He he he

  73. Avatar for Anthony Chambers
    Anthony Chambers May 5th, 2013

    Very useful, thanks. I found that @Model.Message was needed (note casing of 'Model' not 'model').

  74. Avatar for mike
    mike May 26th, 2013

    don't hit the print button it prints a bunch of crap

  75. Avatar for Chamila
    Chamila June 26th, 2013

    this save several hundreds of time.

  76. Avatar for raj
    raj July 23rd, 2013

    it is very usefull

  77. Avatar for Tony
    Tony July 25th, 2013

    Thanks!!!

  78. Avatar for Eric Wardell
    Eric Wardell July 25th, 2013

    This is very nice thanks!

  79. Avatar for Alon Kogan
    Alon Kogan August 12th, 2013

    Niceeee! Thanks.

  80. Avatar for PMacklin
    PMacklin August 19th, 2013

    One thing that threw me for a loop recently was the syntax required for mixing razor statements inside of script tags. This stack overflow question straightened me out: http://stackoverflow.com/qu...

    The syntax looks like this:

    <script type="text/javascript">
    @if (ViewData.ModelState.IsValid)
    {
    <text>$('#ErrorMessage').text('Error');</text>
    }
    </script>
    Without the <text> tags, the view won't compile. The tag is mentioned in your markup guide, but it's very unclear what it is needed for. There are also a few comments which describe it incorrectly - one implies that it gets rendered in the generated markup on the page, but I don't see any cases where it does.

  81. Avatar for zyc
    zyc September 3rd, 2013

    Thank you

  82. Avatar for Dannster
    Dannster October 9th, 2013

    Hi Phil the below code is causing an error in my views since moving to MVC 4



    @foreach (KeyValuePair<string, string> s in subDomains)
    {
    var suburl = @url.Replace("://" + sub, "://" + s.Key);

    var lang = s.Value.Split('-')[0];

    suburl = Html.TranslateUrl(suburl, lang, sub, (IList<Location>)ViewBag.Locations, (IList<CMSPageTranslation>)ViewBag.CMSPageTranslations);

    <link rel="alternate" hreflang="@s.Value" href="@suburl"/>
    }


    The problem I am having is that Razor is snapping back into HTML when it gets to the point where it should be casting the ViewBag.Locations to an IList<Location>

    Do you think this could be a parser error in the new version of Razor?

    I did find a solution however which seems a bit hackish. The solution was to wrap the offending line in a <text>@{ //code here }</text> block

  83. Avatar for William Bosacker
    William Bosacker November 14th, 2013

    I'm trying to find the list of rules that the parser uses to do what you are describing above. Just prior to the release of version 2, the guy who was working on it simplified the syntax for the entire parser down to about 6 or 7 very simple rules. Do you know where I can find those rules? I've lost the link and a web search isn't finding it.

  84. Avatar for Jatin Italiya
    Jatin Italiya November 26th, 2013

    how to exclude @ as operator from the text field for email box

  85. Avatar for MVS
    MVS February 14th, 2014

    The comments inside statement block its only allowed with:
    /* comment */, that is for multi lines comment, and
    // comment, that should be used for single line comment.

    The @* comment *@ can be used only outside statement block

  86. Avatar for jack
    jack March 13th, 2014

    how to write html code in razor mvc4

  87. Avatar for steven6
    steven6 May 21st, 2014

    Agreed, useful reference! Two more syntaxes I like to use are using blocks and closures.

    using blocks: http://www.codeducky.org/ra...

    @using (Html.BeginForm())
    {
    <input type="text" value="input here">
    }

    closures: http://www.codeducky.org/ra...

    @{
    IHtmlString barSpan = Html.Content(@@ViewBag.Bar);
    }
    Bar 1: @barSpan
    Bar 2: @barSpan

    public static IHtmlString Content(
    this HtmlHelper self,
    Func<object, helperresult=""> razorContent)
    {
    return razorContent(null);
    }

  88. Avatar for haacked
    haacked May 23rd, 2014

    Thanks for the examples! I already include the second one. It's called a Razor Delegate.

  89. Avatar for yaron levi
    yaron levi July 7th, 2014

    Great! thanks ! keep coming back to here for updates...

  90. Avatar for Gregory Maier
    Gregory Maier August 18th, 2014

    Thanks for this very useful reference. The only improvement I could suggest if I were asked (and I haven't been) would be to provide a printable version.

  91. Avatar for andresb3
    andresb3 November 26th, 2014

    very useful! thank you

  92. Avatar for QA
    QA January 22nd, 2015

    guys you have MEMORY LEAK on this page it eats up 2GB RAM.

  93. Avatar for SamRay
    SamRay February 2nd, 2015

    Should "view engine" in "Razor view engine" be capitalized? Is "view engine" part of its official name? When I search, I find inconsistent uses.

  94. Avatar for haacked
    haacked February 2nd, 2015

    I don't think so. I think Razor should be capitalized, but "view engine" is generic.

  95. Avatar for Fernando Zamora
    Fernando Zamora February 12th, 2015

    very nice cheat sheet

  96. Avatar for Shimmy
    Shimmy March 16th, 2015

    This is gorgeous!
    Thanks for sharing!!!

  97. Avatar for Maria
    Maria March 27th, 2015

    Very useful :)

  98. Avatar for Joe Buschmann
    Joe Buschmann April 22nd, 2015

    I just started working with MVC and Razor. This is exactly what I was looking for. Thanks!

  99. Avatar for Stephen
    Stephen July 22nd, 2015

    It's 2015 and I'm still using this. So useful!

  100. Avatar for haacked
    haacked July 22nd, 2015

    Thanks!

  101. Avatar for byock
    byock November 18th, 2015

    how can i call a javascript initialize function in razor?

    example:

    <script type="text/javascript">
    var appImageUpload = {
    'initialize': function () { alert("test only to check if this works. I need this function to be inside a variable"); },
    },
    </script>

    @{
    Html.Telerik().ScriptRegistrar().Scripts(s => s.AddSharedGroup("checkScripts")).OnDocumentReady(
    @<text>
    appImageUpload.init2();
    </text>
    );
    }

    -- but it doesnt work.....it isn't throwing the alert that i expect....is my code correct?

  102. Avatar for haacked
    haacked November 30th, 2015

    Looks like you're using something from Telerik. You should contact their support.

  103. Avatar for CJ
    CJ December 11th, 2015

    Nice! I'm going to save this. Thanks a bunch!

  104. Avatar for Yitzhak Andrade
    Yitzhak Andrade July 22nd, 2016

    Nice! Very useful !!

  105. Avatar for Michael White
    Michael White August 22nd, 2017

    By the way, is there somewhere a comparison of home server PCs and server hardware? I want to buy something like IBM 8202 E4C https://datatechcomputer.co..., but on the Internet there is no information about it. I plan to open my own small business. Local store.

  106. Avatar for Pedro René González
    Pedro René González October 19th, 2017
  107. Avatar for Guillermo J Perez
    Guillermo J Perez August 9th, 2018

    how to add a parameter to an html tag?