vVvegetable
vVvegetable

Reputation: 29

how to make DisplayName in MVC model?

while I was building MVC5 model with SQLite. the time span is tricky so I stored the tick instead. but then I get trouble on display name for it.

[PetaPoco.TableName("UserStatus")]
[PetaPoco.PrimaryKey("iUserId", AutoIncrement = true)]
public class User
{
    [Column]
    [DisplayName("User ID")]
    public int iUserId { get; set; }

    [Column]
    [DisplayName("User Name")]
    public string sUserName { get; set; }

    //[DisplayName("Average Time")] this line won't work
    public TimeSpan tsAvgTime;

    [Column]
    public long longAvgTimeTick
    {
        get { return tsAvgTime.Ticks; }
        set { tsAvgTalkTime = new TimeSpan(value); }
    }
}

so what should I do to add a display name instead of change the display on the view.

Upvotes: 0

Views: 173

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

I suspect it is because you have not created Property but a Field.

It should be a property to make it work.

Change it to :

[DisplayName("Average Time")] 
public TimeSpan tsAvgTime {get;set;}

Now this should work.

EDIT:

Just noticed you are using a backing field. You need to apply the attribute on the property which is longAvgTimeTick here and make the field private i.e. private TimeSpan tsAvgTime

so it should be :

private TimeSpan tsAvgTime;

[Column]
[DisplayName("Average Time")]
public long longAvgTimeTick
{
    get { return tsAvgTime.Ticks; }
    set { tsAvgTalkTime = new TimeSpan(value); }
}

Upvotes: 2

Related Questions