asp.net - 某些标记语法破坏剃刀解析
问题描述
下面的代码片段打破了剃刀解析和智能感知,你不知道为什么它不起作用(是的,我确实找到了解决方法,但它仍然困扰着我)。
<select class="form-control" @(ViewBag.View ? "disabled" : string.Empty) asp-for="repetitiveness" asp-items="Html.GetEnumSelectList<TaskRepetitiveness>()">
@if (ViewBag.View)
{
<option selected="selected" value="">@(EnumHelper<TaskRepetitiveness>.GetDisplayValue(Model.repetitiveness))</option>
}
</select>
禁用的案例评估良好,但空字符串案例似乎有问题(以下是使用浏览器测试后的输出):
<select class="form-control" asp-for="repetitiveness" asp-items="Html.GetEnumSelectList<TaskRepetitiveness>()"></select>
这是智能感知在行动:
Ps 它仍然打破了标签助手的大括号:
解决方案
Razor is only valid within a tag when enclosed within quotes and assigned to an HTML attribute. In other words, something like the following should work:
<select class="form-control" disabled="@(ViewBag.View as bool? ?? false)" asp-for="repetitiveness" asp-items="Html.GetEnumSelectList<TaskRepetitiveness>()">
This may seem like it won't work because specifying disabled
at all causes the field to be disabled, but Razor is smart enough to remove the attribute if it's false.
推荐阅读
- python - 如何编写生成以下列表元素的“生成器”函数。列表 = [1,3,4,"hello", 5, 3,4,"create"]
- python-3.x - 测试python浮点范围大小
- vim - Vim 在所有项目文件中搜索
- postgresql - postgresql如何在双引号内插入带有双引号的值
- regex - 用什么正则表达式在两个子字符串之间用换行符替换一些字符串?
- javascript - Three.js 和 CSS3 的结合用于动画与 3D 对象
- rest - 在 API 响应中转发对象的公共属性/变量是一种好习惯吗?
- terraform - terraform vsphere 提供程序在创建 vm 时挂起
- java - Tomcat - 将战争部署到远程 Tomcat 时出现 NoClassDefFoundError
- c# - 模拟服务和网络位置的问题