【发布时间】: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