css - How to use if else blocks to decide style in Razor? -
i know how format css syle of element in razor given condition using ternary operator, in following example:
<div style="@(model.condition ? "float:left" : "float:right")">
but got more complex decision block , should nesting 2 ternary operators, not practice. have tried way:
<div style="@{ if (model.condition) { "float:left" } else { "float:right" } }">
and showing error:
; expected
after strings inside if-else
block, , if add ;
still giving error following
only assignment, call, increment, decrement, , new object expressions can used statement
is there way using if-else
blocks?
you can declare local variable @ begin of view:
@{ var yourstyle = ""; if(model.condition) { yourstyle = "float:left"; } else { yourstyle = "float:right"; //any other conditions , logic } }
and use in div:
<div style="@yourstyle">
Comments
Post a Comment