Jump to content
cunelson3

Razor & HTML with If condition

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!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...