ASP.NET MVC Razor Syntax: This is a razor problem to interpret @if (@Model

I check the exception logs and I ran into an exception in lazy loading with glass cartographer. 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 normally write:

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

      

I tried searching but couldn't find anything. I inherited this code from another guy I can't reach. The website actually loads the page with no errors. I tried the same loop in pure MVC and it loads the page with no error. However, I am getting this lazy loading issue that the object is null in the logs (sitecore).

I'll know more tomorrow, but I'm interested. Has anyone faced such a problem?

+3


source to share


1 answer


I think this works because C # allows the @ symbol at the beginning of a variable name (or any identifier as @recursive in the comment pointed out). In fact it is used to have variable names that are also reserved words. After exiting foreach mode, the executable code will look like this:

foreach(var item in @Model.Collection)

      



And this is valid code in C #

+4


source







All Articles