Neb Neb - 2 years ago 203 Question

Razor if statement inside HTML

Why I can do that:

Dim styleVisible As String = if(Model.IndicateurAchatPeriodique,"block","none")
End Code
<div id="@sectionDCAId" style="display:@styleVisible">...

but not that:

<div id="@sectionDCAId" style="display:@If(Model.IndicateurAchatPeriodique,"block","none")">

I just moved the if instruction inside the HTML but it throw an error. Why the compiler can't understand it?
I don't need solutions, just an explanation. Thanks

Answer Source

The compiler seems to have problems with inline VB.NET Ifs. Not sure if it is a bug or it just isn't quite that clever. I know you haven't asked for a solution, but if you put an extra set of brackets in to help it then it will probably work - as in @(If(Model.IndicateurAchatPeriodique,"block","none"))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download