0

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.

1 Answers1

2

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); }
}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • the longAvgTime is for store in the SQLite, but I don't want to show the long value (it would be ugly and unreadable, eg:60000000000). the shown value is only TimeSpan tsAvgTime so it will be easy to get(eg:01:40:00). but when i display it shows the name tsAvgTime so I want a DisplayName "Average Time" on the html form. – vVvegetable Feb 08 '18 at 15:14