Conditionally apply styling to a table in an MVC Razor view

273 views Asked by At

I have a Partial view that outputs a table. I am calling this Partial from multiple views, one of which is used as an email template.

As you cannot rely on stylesheets when trying to send pretty html emails (as the mail clients ignore them), I want to specify the table's styling attributes in the partial specifically, but only when it is called from a certain view.

I can pass into the ViewData as to whether or not this formatting should be applied, but I am really looking to find out what the easiest/cleanest way to write the 'conditional' partial is.

For example, I could do something like this:

@modeltype ResultsData

@code
    Dim booFullStyling As Boolean = ViewData("FullStyling")

End Code
<h3>Criteria Specified</h3>

<div class="container_criteria">

    <button type="button" class="btn btn-primary btn-xs header hidden-print">
        Expand
    </button>

    <div class="content">

        @If booFullStyling Then
            @<table width="100%" cellpadding="7" style="color: #333;border:#666 medium; font-family: Helvetica, Arial, sans-serif;border-collapse: collapse; border-spacing: 0;">
                ...
            </table>
        Else
            @<table class="table table-condensed table-striped" >
                ...
            </table>
        End If


    </div>
</div>

but I'm repeating the code I write, leaving room for errors.

I could also do something like this:

    <table class="@IIf(booFullStyling,"","table table-condensed table-striped")" style="@iif(booFullStyling,"color: #333;border:#666 medium; font-family: Helvetica, Arial, sans-serif;border-collapse: collapse; border-spacing: 0;","")" >
        ...
    </table>

but this is messy to write and will generate empty class and style properties when not applicable.

I should also mention that I am using VB, not C#.

I have found similar post (like this one) but nothing so far that talks about what I am trying to achieve.

0

There are 0 answers