user2005657
user2005657

Reputation: 627

How to hide a tag based on the value in ViewData

I have a partial view called like:

@Html.Partial("PersonSearch", new ViewDataDictionary { { "searchtypeselectvisible", false } })

In the razor file I have the html:

<div class="searchtypeselect" >
        <input type="radio" name="searchtype" value="person" checked="checked" />Person
        <input type="radio" name="searchtype" value="organisation" class="orgsearchradiobtn" />Organisation
    </div>

I know I can access the data in the razor by using:

@ViewData["searchtypeselectvisible"]

Can someone explain how I can hide the div with class searchtypeselect if the value coming through is false and show it if the value is true?

Upvotes: 1

Views: 2544

Answers (2)

Ravi Gadag
Ravi Gadag

Reputation: 15861

Conditional attribute rendering Conditional attribute rendering

If you have an attribute that might be null, in the past you've needed to do a null check to avoid writing out an empty attribute, like this:

<div @{if (myClass != null) { <text>class="@myClass"</text> } }>Content</div>

Now Razor is able to handle that automatically, so you can just write out the attribute. If it's null, the attribute isn't written:

<div class="@myClass">Content</div>

So if @myClass is null, the output is just this:

<div>Content</div>

try like this

@(ViewData["searchtypeselectvisible"]==true ? "yourDisplayClass" : "yourHidingDivClass");

Upvotes: 2

Kleber Bernardo
Kleber Bernardo

Reputation: 193

Use @{ if{ } } to set attribute style with display: none, easy.

Upvotes: 1

Related Questions