I'd like to have this html code where a condition ("myBool") is true :
<div>
<fieldset>
<legend>
@sometext
</legend>
... other stuffs
</fieldset>
<div>
and this one when it's false :
<div>
<b>
@sometext
</b>
... other stuffs
<div>
I dont what to whave to write se same code ("other stuffs") twice so I tried this :
<div>
@if(myBool)
{
<fieldset>
<legend>
}
else
{
<b>
}
@sometext
if (myBool)
{
</legend>
}
else
{
</b>
}
...other stuff
if (myBool)
{
</fieldset>
}
</div>
but I get compilation errors.
Do you see how I could do what I want without having to do somethig like that :
@if(myBool)
{
<div>
<fieldset>
<legend>
@sometext
</legend>
... other stuffs
</fieldset>
<div>
}
else
{
<div>
<b>
@sometext
</b>
... other stuffs
<div>
}
Thank you.
The following might work using the @:
operator.
When you're inside a code block, either because of a control structure (like in the example given below) or because you have explicitly defined one, you can output plain text by prefixing with a @-character followed by a : (colon) character.
<div>
@if (someCondition)
{
@:<fieldset>
@:<legend>
}
else
{
@:<b>
}
@sometext
if (someCondition)
{
@:</legend>
}
else
{
@:</b>
}
... other stuffs
@if (someCondition)
{
@:</fieldset>
}
</div>
or you might also try the following:
<div>
@Html.Raw(someCondition ? "<fieldset><legend>" : "<b>")
@sometext
@Html.Raw(someCondition ? "</legend>" : "</b>")
... other stuffs
@if (someCondition)
{
@:</fieldset>
}
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With