views:

63

answers:

1

I cannot figure out how to do "nested" operation in Razor. For example how to use IF inside FOREACH. VisualStudio throws compile-time error on following block, saying "Invalid expression term 'if' "

@foreach (var document in Model) {

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

}
+5  A: 

Don't you just need to drop the @ off the @if and make it:

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

Sorry I haven't worked with Razor but isn't its selling point the automatic switching back and forth between code and HTML based on context?

Carson63000