【问题标题】:Razor broken when a space between HTML’s attribute and its value当 HTML 属性与其值之间有空格时,剃刀损坏
【发布时间】:2016-03-16 15:51:44
【问题描述】:

我有一个这样的ViewBag

ViewBag.ApplyDiscount = false;
ViewBag.ExpressShip = true;
ViewBag.Supplier = null;

还有一些cshtmlsn-ps 这样的

Discount:<input type="checkbox" checked="@ViewBag.ApplyDiscount"/>
Express:<input type="checkbox" checked="@ViewBag.ExpressShip"/>
Supplier:<input type="checkbox" checked="@ViewBag.Supplier"/>

razor 渲染cshtmlsn-ps 后,真正的html 将是

Discount:<input type="checkbox"/>
Express:<input type="checkbox" checked="checked"/>
Supplier:<input type="checkbox"/>

但如果我在checked 属性和=like 之间添加一个空格

Discount:<input type="checkbox" checked ="@ViewBag.ApplyDiscount"/>
Express:<input type="checkbox" checked ="@ViewBag.ExpressShip"/>
Supplier:<input type="checkbox" checked ="@ViewBag.Supplier"/>

razor 会以错误的方式渲染cshtml sn-p。 html 会是这样的:

Discount:<input type="checkbox" checked ="False"/>
Express:<input type="checkbox" checked ="True"/>
Supplier:<input type="checkbox" checked =""/>

无论是 MVC4(VS2012) 还是 MVC5(VS2015),都会发生这种情况。

那么,谁能告诉我为什么空间会导致这件事发生?

【问题讨论】:

  • 您使用checked="@ViewBag.ExpressShip" 生成checked="checked" 是剃须刀的一项功能,称为MVC-4 中引入的条件属性。更多详情请参考this article

标签: asp.net-mvc razor


【解决方案1】:

因为检查本身是旧版 HTML 中的一个特殊的独立布尔属性,并且可以是没有属性值的有效语法。

它的存在表明该框已被选中。

例如

<input type="checkbox" checked />

<input type="checkbox" checked="true" />

以同样的方式执行。这就是为什么在 jquery 中我们总是使用 :checked 选择器。它混淆了检查变化的需要。

【讨论】:

  • 感谢您的回答!!!!这对我理解这个问题有很大帮助。所以,这不是一个剃刀虫。
  • 我的荣幸。祝你的应用好运!
猜你喜欢
  • 2011-10-27
  • 2016-04-29
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2010-10-24
  • 2014-05-11
  • 2017-03-30
  • 1970-01-01
相关资源
最近更新 更多