55

Here is what I have at the moment

 hidden="@(Model.IsOwnedByUser||!Model.CanEdit)"

This works fine on Chrome but doesnt hide on Internet Explorer

I tried also visibility set false but no luck.

then I found out another style as below

style="@(Model.IsOwnedByUser||!Model.CanEdit)?'display:none'""

I could not get it worked. What is the correct format to hide an element with Razor syntax?

Or I would use Jquery to hide the element. but is it actually possible print out jquery statement that would hide the element on page load?

3
  • What is wrong with the second statement? except the fact that you have two " at the end and are missing an 'else' (:'') Commented Feb 26, 2014 at 15:42
  • style="@(Model.IsOwnedByUser || !Model.CanEdit)?'display:none':'display'" when I see the source code Commented Feb 26, 2014 at 15:58
  • this is the return : style?'display:none':'display' Commented Feb 26, 2014 at 16:00

5 Answers 5

110

The below code should apply different CSS classes based on your Model's CanEdit Property value .

<div class="@(Model.CanEdit?"visible-item":"hidden-item")">Some links</div>

But if it is something important like Edit/Delete links, you shouldn't be simply hiding,because people can update the css class/HTML markup in their browser and get access to your important link. Instead you should be simply not Rendering the important stuff to the browser.

@if(Model.CanEdit)
{
  <div>Edit/Delete link goes here</div>
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I was missing a syntax when I have 2 conditions.
<div style="display:@(Model.CanEdit ? "block":"none" )"></div> you can also use above code to show or hide html element.
Second method is using logic in view which is not an appreciated method.
42

Try:

<div style="@(Model.booleanVariable ? "display:block" : "display:none")">Some links</div>

Use the "Display" style attribute with your bool model attribute to define the div's visibility.

4 Comments

Please format your answer to improve its readability. Explaining why this works and/or providing references would also improve your answer.
I shouldn't prefer using this for sensitive information. Because Display:none can be disabled in a browser through Inspect Element. using an @if statement as stated in Shyju's answer is safer.
@Steven, We can still change the class value through inspect... or can change the display property from class name.. so hardly matters
If you place display:none either in style or in a css class, it'll still be rendered. You would want to prevent the div from rendering,
4

Your code isn't working, because the hidden attibute is not supported in versions of IE before v11

If you need to support IE before version 11, add a CSS style to hide when the hidden attribute is present:

*[hidden] { display: none; }

Comments

4

I know the question is specifically about hiding the div but if you don't do anything else with the div (in JavaScript for example) then you can just not include it in the output of the view by using the Razor @if syntax.

@if (Model.IsOwnedByUser && Model.CanEdit)
{
<div>Some Links</div>
}

Comments

1

This seemed to work for me....

<div style="@(Model.YourBoolean?"":"display: none;")">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.