Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.LabelFor Specified Text

Anyone got any idea on how to specify text when using Html.LabelFor(c=>c.MyField). It's just MyField might not be an appropriate name to display on screen, you may want "The Super Fantastic Field" instead, but there doesn't appear to be any overloads.

Any ideas?

like image 914
Kezzer Avatar asked Aug 21 '09 16:08

Kezzer


5 Answers

You use System.ComponentModel.DataAnnotations.DisplayAttribute:

[Display(Name = "My Field")]
public string MyField { get; set; }

Setting the ResourceType property on your attribute will allow you to use a resource file.

(Prior to .NET 4 use System.ComponentModel.DisplayNameAttribute with the caveat that the display name must be a compile-time constant.)

like image 55
Curtis Buys Avatar answered Sep 27 '22 19:09

Curtis Buys


Easy solution just add the following in the view:

@Html.LabelFor(c=>c.MyField, "My Field")
like image 43
Faisal Khalid Avatar answered Sep 27 '22 17:09

Faisal Khalid


There is a new overload in MVC 3 so you should be able to specifiy custom test for the labelfor helper.

like image 40
Joe Cartano Avatar answered Sep 27 '22 19:09

Joe Cartano


I haven't downloaded v2 yet, so I can't test, but I believe it works like DynamicData, in which case you'd do something like this on your model:

[Display(Name = "The Super Fantastic Field")]
public string MyField {get;set;}
like image 41
Daniel Avatar answered Sep 27 '22 18:09

Daniel


There are two ways
1"direct annotations"
2"Annotatinos with a resource"
Direct annotations

[Display(Name = "My Field")]
public string MyField { get; set; }

Annotatinos with a resource

[Display(Name = "My_Field",ResourceType = typeof(Resource))]
public string MyField { get; set; }

Second way will require to add a value in resource file probably named as Resource.resx.
Use which suits your purpose.

like image 38
Baimyrza Shamyr Avatar answered Sep 27 '22 17:09

Baimyrza Shamyr