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

早过忘川 提交于 2019-12-18 01:42:10

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/36290189/what-does-mean-in-asp-net-mvc-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!