如何根据模型值隐藏 div 元素?MVC [英] How to hide a div element depending on Model value? MVC

查看:23
本文介绍了如何根据模型值隐藏 div 元素?MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前所拥有的

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

这在 Chrome 上运行良好,但在 Internet Explorer 上不隐藏

This works fine on Chrome but doesnt hide on Internet Explorer

我也尝试将可见性设置为 false 但没有运气.

I tried also visibility set false but no luck.

然后我发现了另一种样式如下

then I found out another style as below

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

我无法让它工作.使用 Razor 语法隐藏元素的正确格式是什么?

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

或者我会使用 Jquery 来隐藏元素.但实际上是否可以打印出在页面加载时隐藏元素的 jquery 语句?

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?

推荐答案

以下代码应根据您模型的 CanEdit 属性值应用不同的 CSS 类.

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>

但是如果它是像编辑/删除链接这样重要的事情,你不应该简单地隐藏,因为人们可以在他们的浏览器中更新 css 类/HTML 标记并访问你的重要链接.相反,您应该简单地不渲染浏览器的重要内容.

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>
}

这篇关于如何根据模型值隐藏 div 元素?MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆