Nested operations with the Razor View Engine

I cannot figure out how to do a โ€œnestedโ€ operation in Razor. For example, how to use IF inside FOREACH. VisualStudio throws a compilation error in the next block saying "Invalid expression term" if "

@foreach (var document in Model) { @if (document.Item.Count > 0) { <div> @MvcHtmlString.Create(document.Items[0].ContentPresenter) </div> } } 
+6
c # asp.net-mvc-3 razor
source share
1 answer

No need to just drop @ with @if and do this:

 @foreach (var document in Model) { if (document.Item.Count > 0) { <div> @MvcHtmlString.Create(document.Items[0].ContentPresenter) </div> } } 

Sorry I didnโ€™t work with Razor, but is its selling point an automatic context-based switch between code and HTML?

+14
source share

All Articles