super cool
super cool

Reputation: 6045

Binding view using model data minor issue ? MVC4

I am trying to bind the model class to my view i.e strongly typed . My model class :

 public class LeadSortModel
    {
        public string Contact_Name { get; set; }
        public string Contact_Address { get; set; }

In my view i am trying like this :

@Html.LabelFor(m=>m.Contact_Name) 
 @Html.LabelFor(m=>m.Contact_Address)

On my OUTPUT screen I am getting like Contact_address : Contact_name : ..... But instead i want to get Contact Address with a space and no '_' in between .

In model class properties with a space in b/w is not possible so i am looking for some better alternative does my need.

Any work around is quite helpful . Else shall i go Like ContactAddress and ...

Thanks & Regards

Upvotes: 0

Views: 31

Answers (2)

ssilas777
ssilas777

Reputation: 9804

You can use [DisplayName("")] from System.ComponentModel namespace attribute for this

  public class LeadSortModel
            {
            [DisplayName("Contact Name")]   
            public string Contact_Name { get; set; }
            [DisplayName("Contact Address")] 
            public string Contact_Address { get; set; 

In the view

@Html.LabelFor(m=>m.Contact_Name) 

will generate

<label for="Contact_Name">Contact Name</label>

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Use DisplayName attribute of DataAnnotations like this:

public class LeadSortModel
    {
        [DisplayName("Contact Name")]
        public string Contact_Name { get; set; }
        [DisplayName("Contact Address")]
        public string Contact_Address { get; set; }
     }

Upvotes: 1

Related Questions