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

前端 未结 1 1486
旧巷少年郎
旧巷少年郎 2020-12-08 13:37

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


    @:

相关标签:
1条回答
  • 2020-12-08 14:11

    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)

    0 讨论(0)
提交回复
热议问题