Search code examples
asp.net-mvcrazor

What does @: mean in ASP.net MVC Razor?


I'm working on an ASP.net MVC Razor view that someone else wrote. I see that it contains the following:

<span>
    @:
</span>

I know that the @ symbol allows me to insert code into a view, but what does @: stand for?


Solution

  • In MVC, @ is the respective char that allows you to use razor inside HTML (inside a .cshtml) which in runtime (or precompiled) will be converted to c#.

    With @ you may write C# within HTML and with @: you may write HTML within C#.

    Example:

    @foreach (TestClass item in Model)
    {
        @:@item.Code - @item.Name
    }
    

    Without the @: it wouldn't be possible to do this, since all the chars after the first @ will be considered as C#.

    This way you are saying that you are getting the two variables from item and placing the char - between them and the result is a content block (or html/text)