最新版本的 Visual Studio 2015 使我们的整个 cshtml 标记处于非常脆弱的状态。很多时候,我们的属性值超出了可用宽度,我们需要将属性拆分为多行,以使代码更具可读性。
示例:宽数据绑定(bind)
<div data-bind="someBinding: { hasErrors: bindingObject.HasErrors(duration), hasWarnings: bindingObject.HasWarnings(duration), parameterKey: bindingObject.SelectedObjectKey }, anotherBinding: value, anotherBinding2: value2, AndSoOn: yaddayadda.the.point.is.this.can.get.really.long">
回到 VS 2010/2013,如果我们开始只是在属性值中放入回车符来将其分解,设计器通常将不再正确解析 html,并且此后的 DOM 元素将不再有效......例如 table 坏了。
因此我们发现,通过将属性值封装在 @("...") 中实际上可以让设计器顺利运行。
示例:使用 @() 属性绑定(bind)
<div data-bind="@("someBinding: { " +
"hasErrors: bindingObject.HasErrors(duration), " +
"hasWarnings: bindingObject.HasWarnings(duration), " +
"parameterKey: bindingObject.SelectedObjectKey }, " +
"anotherBinding: value, " +
"anotherBinding2: value2, " +
"AndSoOn: yaddayadda.the.point.is.this.can.get.really.long")">
好消息是 IDE 甚至可以帮助扩展这个字符串,当页面重新格式化时它会很高兴并且 DOM 状态良好。服务器将获取此 C# 字符串并将其渲染到生产中的单行,一切都很好...
直到我们升级。 VS2015 似乎讨厌这一点,但原因完全不同。现在它会显示整个内容,并且工具提示显示“缺少属性名称”。当页面重新格式化时,一切都会变得糟糕......
所以我在这里走在玻璃上。这是某种错误还是有答案,因为我们有一个庞大的代码库,不想重构此属性格式。
想法?
最佳答案
Visual Studio 2015 Update 1 包含此问题的解决方案。
关于razor - 如何在 Visual Studio 2015 中编写多行属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33681156/