简体   繁体   中英

ASP.NET MVC Razor Syntax: Is it an issue for razor to interpret @if(@Model

I am checking logs of exception and I came across an exception in lazy loading with glass mapper. I have a sitecore project with mvc and one of the views (shared cshtml) contains the following loop:

@foreach(var item in @Model.Collection)...

I would usually write:

 @foreach(var item in Model.Collection)...

I tried googling, but I found nothing. I have inherited this code from another guy, who I cannot contact. The website actually loads the page with no error. I've tried the same loop in pure MVC and it loads the page with no Error. However, I get this lazy loading issue that the entity is null there in logs (sitecore).

I will know more tomorrow, but I am curious regarding this. Has anyone came across such issue?

I guess it works because C# allows the @ character at the beggining of a variable name (or any identifier, as @recursive pointed out in a comment). Actually this is used to allow having variable names that are also reserved words. After escaping the foreach, the code executed will look like:

foreach(var item in @Model.Collection)

And this is valid code in C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM