在有条件的属性布尔字符串比较MVC4 /剃刀2返回意外的结果 [英] Boolean string comparison in conditional attribute with MVC4 / Razor 2 returns unexpected results
问题描述
升级我的MVC 3解决方案,以MVC 4剃刀2后,我遇到了以下问题。
After upgrading my MVC 3 solution to MVC 4 with Razor 2 I came across the following problem.
这code在视图
@{
string main = "archive";
}
<div class="selected-@(main == "archive")"></div>
在MVC 3返回此
<div class="selected-True"></div>
而这MVC 4
and this in MVC 4
<div class="selected-class"></div>
它打破我的CSS。
which breaks my CSS.
是由新的条件属性功能在剃刀2中引入一个错误?
Is that a bug introduced by the new conditional attribute feature in Razor 2?
我的解决方法是这样的:
My workaround is this:
<div class="selected-@((main == "archive").ToString())"></div>
返回这样的:
<div class="selected-True"></div>
有没有人有更好的建议?
Does anyone have a better suggestion?
推荐答案
我的错误有或没有的问题是在这里得到解答: HTTP: //stackoverflow.com/a/13455272/533460
My bug-or-not question is answered here: http://stackoverflow.com/a/13455272/533460
这不是一个错误,这是剃刀2的新功能!
It's not a bug, it's a new feature of Razor 2!
在什么我的code的情况是基于这个原理
What is happening in my code is based on this principle
<input checked="@ViewBag.Checked" type="checkbox"/>
变为
<input checked="checked" type="checkbox"/>
在ViewBag.Checked ==真。
when ViewBag.Checked == true.
所以
<div class="@(main == "archive")"></div>
变为
<div class="class"></div>
由于@webdeveloper(<一个href=\"http://stackoverflow.com/users/1667969/webdeveloper\">http://stackoverflow.com/users/1667969/webdeveloper)指着我的答案。
这篇关于在有条件的属性布尔字符串比较MVC4 /剃刀2返回意外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!