简体   繁体   中英

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:

[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.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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