Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a div element depending on Model value? MVC

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?

like image 607
akd Avatar asked Feb 26 '14 15:02

akd


People also ask

How do I hide a specific div?

We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.

How do I hide an element without removing Dom?

Try setting display:none to hide and set display:block to show. not all items should be display: block , some will have to be set to display: inline (such as <span> or <a> or <img> DOM elements). @pranay the question says to hide them not to remove them from the DOM.

How do you hide an element style?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden. display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.


2 Answers

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> } 
like image 127
Shyju Avatar answered Oct 07 '22 17:10

Shyju


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.

like image 45
Yuri Avatar answered Oct 07 '22 16:10

Yuri