Jump to content

Razor & HTML with If condition


cunelson3

Recommended Posts

I'm practicing and trying to write a table varying the opening row tags:

 

@foreach(blah in blahs)

{

if (condition){

@Html.Raw('<tr>');

}

else {

@Html.Raw('<tr class="shaded">');

}

 

// More row stuff here

 

</tr>

}

 

Is there a way to do this? My editor keeps complaining about a missing <tr> tag. Is it possible to add a class to a tag using razor? What's the common practice?

 

BTW, this is an incredible web site!

Link to comment
Share on other sites

Its probably complaining because it sees the HTML </tr> tag, but does not see html <tr> tag to go with it, because it is produced by coding. But! If you then produce </tr> by coding it will then complain that there is no <tr> OR </tr>, if you can code it to just insert class when required in <tr> tag instead

 

@foreach(blah in blahs)
{

variable = '';
     if (condition){

          variable='class="shaded"';

     }

    <tr insert variable>

    // More row stuff here

    </tr>

}
It would have less reason to complain.
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...