celticharp
celticharp

Reputation: 155

How to nest divs inside if statement

I want to make something like that:

     @if ( some-statement )
     {
        <div class="row-link">
     }
     else
     {
        <div class="row">
     }
     <div class="new">some content</div>
     </div>

but the compiler keeps telling me that the if has no closing character! What's wrong? Thanks!

Upvotes: 0

Views: 4541

Answers (4)

ZippyV
ZippyV

Reputation: 13028

I'm not fluent in c# but why don't you use a variable to hold the classname?

@string classname;
@if ( some-statement ) { 
    classname = "row-link";
} else {
    classname = "row";
}
<div class="@classname">
    <div class="new">some content</div>
</div>

Or you could use a helper function. It keeps your html cleaner.

Upvotes: 4

Abdul Munim
Abdul Munim

Reputation: 19217

You may consider closing your <div> and remove your common end </div>

 @if ( some-statement )
 {
    <div class="row-link"></div>
 }
 else
 {
    <div class="row"></div>
 }
 <div class="new">some content</div>

Upvotes: 0

zynaps
zynaps

Reputation: 471

try

@if ( some-statement )
{
   @:<div class="row-link">
}
else
{
   @:<div class="row">
}
<div class="new">some content</div>
</div>

Upvotes: 1

dknaack
dknaack

Reputation: 60486

Use <text>

@if ( some-statement)
 {
    <text>
    <div class="row-link">
    </text>
 }
 else
 {
    <text>
    <div class="row">
    </text>
 }
 <div class="new">some content</div>
 </div>

and check out

http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

hope this helps

Upvotes: 2

Related Questions