Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do inline Razor code within some HTML?

What I want to do:

<div class='duck@if (this.Model.quacker) { -noisy }'>quack</div>

ie. either

<div class='duck'>quack</div>

or

<div class='duck-noisy'>quack</div>

However I can't quite get the syntax right. I tried @:-noisy but that generates errors such as

Make sure you have a matching "}" character for all the "{" characters within this block

I tried also @:-noisy@ but the same error. What should I do?

like image 262
NibblyPig Avatar asked Sep 23 '11 10:09

NibblyPig


2 Answers

The <text> element will transition to HTML again. Like this:

<div class='duck@if (this.Model.quacker) { <text>-noisy</text> }'>quack</div>
like image 59
Arjan Einbu Avatar answered Oct 06 '22 00:10

Arjan Einbu


should be like this

<div @if(this.Model.quacker) 
{
@:class='duck-noisy' 
}else{
@:class='duck'
}>quack</div>
like image 26
Frederiek Avatar answered Oct 05 '22 23:10

Frederiek