86

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?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Kieran Senior
  • 17,960
  • 26
  • 94
  • 138

7 Answers7

143

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

Curtis Buys
  • 2,341
  • 1
  • 17
  • 13
  • 9
    You're the winner. You've got to have `using System.ComponentModel;` though. – Kieran Senior Aug 24 '09 at 08:19
  • I have my code generator spit these out automatically, inserting spaces between Pascal-cased words. Works like a charm! – GalacticCowboy May 19 '10 at 20:20
  • Although this solution looks quite nice, I get `Error 381 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type`. So this solution seems to be bad for l18n. – Martin Thoma Jan 26 '13 at 11:35
  • @moose I've updated the answer to reflect the new-in-.NET-4 [`System.ComponentModel.DataAnnotations.DisplayAttribute`](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.aspx) which allows the use of a resource file. If you're not yet on 4/4.5, the old [`System.ComponentModel.DisplayNameAttribute`](http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute.aspx) can be extended like so: http://stackoverflow.com/a/2432520/33533 – Curtis Buys Feb 17 '13 at 23:20
  • @CodeBlend I was using T4 to generate the model classes with appropriate attributes. I had a method that split the string on capital letters and then joined again with a space. (It's been 3 years, so don't have the code in front of me at the moment...) – GalacticCowboy Apr 25 '13 at 17:10
  • I have several issues with this. I think this is a good solution, however, check this page for another approach. https://sheldons.me/2014/10/06/mvc-5-placeholders-prompts-and-watermarks/ – Juan Acosta Jul 16 '17 at 06:44
56

Easy solution just add the following in the view:

@Html.LabelFor(c=>c.MyField, "My Field")
Faisal Khalid
  • 661
  • 5
  • 3
26

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

Joe Cartano
  • 2,997
  • 3
  • 22
  • 40
3

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;}
Daniel
  • 1,516
  • 1
  • 13
  • 24
  • Display isn't available, and intellisense can't find it in any libs either. – Kieran Senior Aug 21 '09 at 16:33
  • Add a reference to System.ComponentModel.DataAnnotations.dll and add using System.ComponentModel.DataAnnotations. – Daniel Aug 21 '09 at 18:23
  • Rather, add reference to System.ComponentModel.DataAnnotations from the GAC (ie in the .NET tab of add references) – Daniel Aug 21 '09 at 18:24
  • I use DataAnnotations already, so the reference isn't the problem. – Kieran Senior Aug 22 '09 at 20:40
  • 3
    Ah, sorry - that's apparently Silverlight3/.NET 4.0: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute%28VS.100%29.aspx I think DisplayName is going to do what you want, as itsmecurtis suggests. Here's more on that: http://davidhayden.com/blog/dave/archive/2009/08/19/DisplayNameAttributeInMVC.aspx – Daniel Aug 23 '09 at 20:43
2

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.

Baimyrza Shamyr
  • 449
  • 1
  • 6
  • 17
0

I haven't checked out CP1 yet but I read over Scott's release of it and I seem to recall that the code was generated by T4. I suppose you could always mod that, but I would suspect that they will provide overloads in CP2.

Edit: The source is always available and thus you could just mod the method, change the T4 generator, and you'll be good to go. Also put in a ticket or request (somehow) for that mod so it gets worked into the next version.

Chance
  • 11,043
  • 8
  • 61
  • 84
  • Shame, I'm doing a work preview but it's not really viable as our field names are quite spurious compared to their description counterparts. – Kieran Senior Aug 21 '09 at 16:20
0

There are 5 overloads. Several offer second parameter of "string labelText", which you would set to "The Super Fantastic Field".

yogibeare
  • 41
  • 1
  • 3