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

前端 未结 3 1145
情书的邮戳
情书的邮戳 2020-12-09 00:57

Here is what I have at the moment

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

This works fine on Chrome but doesnt hide on Internet

相关标签:
3条回答
  • 2020-12-09 01:18

    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; }
    
    0 讨论(0)
  • 2020-12-09 01:43

    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>
    }
    
    0 讨论(0)
  • 2020-12-09 01:44

    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.

    0 讨论(0)
提交回复
热议问题