简体   繁体   English

当我使用 DataAnnotations 和 DisplayName 时 LabelFor 不起作用 - ASP.NET MVC

[英]LabelFor doesn't work when I use DataAnnotations and DisplayName - ASP.NET MVC

I have a folder called "ModelMetaData.cs" where I have a class called "InspeccioModelMetaData.cs" with these code:我有一个名为“ModelMetaData.cs”的文件夹,其中有一个名为“InspeccioModelMetaData.cs”的类,其中包含以下代码:

[DisplayName("Id")]
public string IdInspeccio { get; set; }
[DisplayName("Data de caducitat")]
public DateTime DataCaducitat { get; set; }

And the Model have these code:模型有这些代码:

[MetadataType(typeof(InspeccioModelMetaData))]
public partial class Inspeccio
{

... ...

public System.DateTime DataCaducitat { get; set; }

... ...

In the view when I use @Html.DisplayNameFor it works but when I use在我使用 @Html.DisplayNameFor 的视图中它有效但是当我使用

@Html.LabelFor(model => model.DataCaducitat, htmlAttributes: new { @class = "control-label col-md-2" })

it doesn't.它没有。

I have searched the solution by googling but nothing works for me...我已经通过谷歌搜索搜索了解决方案,但对我没有任何作用......

Can someone helps me please?有人可以帮我吗?

Thanks!谢谢!

Make sure that these metadata classes are in the same namespace as the model.确保这些元数据类与模型位于同一命名空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM