Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.NET MVC, how do I display a property name as a label splitting on CamelCase

I know that I have seen this before. I can't remember if it was a C4MVC template demo or an input builder thing! I need to know how I can use the convention of CamelCasing my view model properties and having the "CamelCasedProperty" rendered in the label as "Camel Cased Property". This should be handled by the create new view wizard rather than programatically handling this.

like image 914
Andrew Siemer Avatar asked Apr 27 '10 00:04

Andrew Siemer


People also ask

Which attribute can be applied to a property of a model to specify the label name in a view?

The label is specified by using the Label Attribute applied to the property.

What is named section in MVC?

In ASP.NET MVC, a section is a piece of code that we want to render in a layout page. This allows us to render a specific view content in any location of a layout. In order to declare a section in a view, we use the @section keyword followed by the name of the section. @section name { }


1 Answers

I don't think what you want is possible in vanilla ASP.NET MVC 2.

In ASP.NET MVC 2 you need to decorate your model with a DisplayName attribute with the text you want and then the auto generated wizard will use LabelFor to output the label for the property. Eg:

class MyModel()
{
    [DisplayName("Your Property Name")]
    public string YourPropertyName { get; set; }
}

Then in the view:

<%= Html.LabelFor(m => m.YourPropertyName) %>

If you saw a demo of it being done some other way it could have been from the MvcContrib project for InputBuilders.

Here is direct link to the part of the project I think your referring to:

http://www.lostechies.com/blogs/hex/archive/2009/06/09/opinionated-input-builders-for-asp-net-mvc-part-2-html-layout-for-the-label.aspx

The text highlighted in red are labels that come from the Model type. The label is created from the PropertyInfo object that represents the respective properties of the mode.

The label is Property Name.

The label is the Property Name that is split on the pascal case property name.

The label is specified by using the Label Attribute applied to the property.

like image 139
Kelsey Avatar answered Oct 14 '22 19:10

Kelsey