views:

138

answers:

3

If I have a table Orders with fields CustomerID, OrderID and OrderDate, then the "Linq-to-SQL classes" generated class will be called Orders, with members called CustomerID, OrderID and OrderDate. So far so good.

However, if I then do Html.LabelFor(m => m.OrderDate) then the generated text will be "OrderDate" instead of "Order Date".

I tried using Order_Date as the field name, but that didn't work. Is there any way to get it to infer a better display name?

[I know that I can use data annotations to specify the display name explicitly, but I really don't want to do that for all my classes/members - I just want it to work by convention.]

+2  A: 

I suggest you create your own HTML Helper for this, something like Html.MyLabelFor.

The rules to apply from here are up to you. You can simply split the word by case.

bruno conde
Agreed. "by convention" is pretty much going to mean you have to do it yourself
Russell Steen
Even so, the "convention" in C#/VB is to name properties using camel-casing. For a method with the sole purpose of displaying a human-readable version of the property, I'd almost expect it to already do this. By convention.
James Kolpack
Ok, thanks. I'll mark this as an answer, even though it's not the answer I was looking for!
Gary McGill
@James: I presume you mean PascalCase, not camelCase?
Gary McGill
@Gary McGill - True!
James Kolpack
@Gary McGill Check out my answer below. It adds your convention to the MVC 2 framework instead of forcing you to write HTML helpers.
jfar
+2  A: 

There is a solution available for your requirements contained within the answer to this question. Asp.Net MVC 2 LabelFor Custom Text.

Nicholas Murray
+1  A: 

This method takes advantage of existing MVC 2 architecture to place conventions over the entire *For rendering methods instead of one off HTML helpers and without having to re-label everything with spaced property names.

http://stackoverflow.com/questions/2269144/how-to-dry-up-c-attributes-in-models-and-viewmodels/2270115#2270115

Essentially what your doing is overriding the default ConventionModelMetadataProvider behavior of MVC 2 and providing hooks for you to insert your own opinionated conventions.

jfar
@jfar: I was really hoping for something out-of-the-box, but this is the next best thing since at least it follows the extensibility path (and is preferable to doing a complete re-implementation of *For).
Gary McGill