一个新的 MVC 元素包含一个 Site.css,它指定:
/* Styles for editor and display helpers
----------------------------------------------------------*/
.display-label,
.editor-label
{
font-weight: bold;
margin: 1em 0 0 0;
}
但是,当我使用 LabelFor(m=>m.SomeField)
生成的 html 不包含类属性:
<label for="SomeField">Some Field</label>
我见过其他示例,其中人们使用 LabelFor 并且生成的 html 确实包含 class="display-label"
属性。
LabelFor 是否应该生成此类属性?如果是,为什么我的不是?
我确实连接了一个自定义的 DataAnnotationsModelMetadataProvider,但它仍在调用基础:
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
if (metadata.DisplayName == null)
metadata.DisplayName = propertyName.ToTitleCaseFromCamel();
return metadata;
}
最佳答案
这些 css 类仅在您使用 EditorFor 和 DisplayFor 模板时存在。仅使用 LabelFor 时,您需要提供自己的 css 类。
关于css - LabeFor 不生成显示标签类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934635/