i trying display sql server time field in , pm. displays correcly in edit mode not in list mode. model:
public class hmsappointmentmetadata { public int id { get; set; } [required(errormessage = "rep name required")] [stringlength(50, errormessage = "max length 50")] public string rep { get; set; } [required(errormessage = "customer name required")] [stringlength(50, errormessage = "max length 50")] public string name { get; set; } [required(errormessage = "address required")] [stringlength(100, errormessage = "max length 100")] public string address { get; set; } [required(errormessage = "city required")] [stringlength(50, errormessage = "max length 50")] public string city { get; set; } [required(errormessage = "zip required")] [stringlength(50, errormessage = "max length 50")] public string zipcode { get; set; } [required(errormessage = "county required")] [stringlength(50, errormessage = "max length 50")] public string county { get; set; } [required(errormessage = "home phone required")] [stringlength(10, errormessage = "max length 10")] public string homephone { get; set; } [required(errormessage = "mobile phone required")] [stringlength(10, errormessage = "max length 10")] public string cellphone { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string ownhome { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string haveattic { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string avgelectric { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string summerspike { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string triedany { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string insulationtype { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string insulationage { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string lastpaint { get; set; } [required(errormessage = "question required")] [stringlength(50, errormessage = "max length 50")] public string married { get; set; } public string spouse { get; set; } [required(errormessage = "occupation required")] [stringlength(50, errormessage = "max length 50")] public string job { get; set; } public string spousejob { get; set; } public string retirefrom { get; set; } public string spouseretirefrom { get; set; } [required(errormessage = "appointment date required")] [datatype(datatype.date)] [displayformat(dataformatstring = "{0:yyyy-mm-dd}", applyformatineditmode = true)] public nullable<system.datetime> date { get; set; } [required(errormessage = "appointment time required")] [datatype(datatype.time)] [displayformat(dataformatstring = "{0:hh\\:mm}", applyformatineditmode = true)] public nullable<system.timespan> time { get; set; } [displayformat(dataformatstring = "{0:hh\\:mm tt}")] public system.timespan? timefordisplay { { return (time.hasvalue) ? (system.timespan?)datetime.now.timeofday.add(time.value) : null; } } public hmsappointmentmetadata() { time = new timespan(14,30,0); } [datatype(datatype.multilinetext)] public string comments { get; set; } public string creditscore { get; set; } } list view:
<table class="table"> <tr> <th> @html.displaynamefor(model => model.name) </th> <th> @html.displaynamefor(model => model.city) </th> <th> @html.displaynamefor(model => model.date) </th> <th> @html.displaynamefor(model => model.time) </th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.name) </td> <td> @html.displayfor(modelitem => item.city) </td> <td> @html.displayfor(modelitem => item.date) </td> <td> @html.displayfor(model => model.timefordisplay) </td> <td> @html.actionlink("notes", "index", "adminnotes", new { appointmentid = item.id }, null) | @html.actionlink("edit", "edit", new { id = item.id }) | @html.actionlink("details", "details", new { id = item.id }) | @html.actionlink("delete", "delete", new { id = item.id }) </td> </tr> } </table> this results in 24 hr time (14:30)
edit view:
@html.editorfor(model => model.time) this displays correctly formatted time(2:30 pm).
i know not ideal without little bit of work not working here options
option 1
use displaytemplate , format manually.
option 2 -- better.
add new property viewmodel
[displayformat(dataformatstring = "{0:hh\\:mm tt}")] public datetime? timefordisplay { { return (time.hasvalue)? (datetime?)datetime.today.add(time.value):null;} } use new property display in view follows
@html.displayfor(model => model.timefordisplay) here demo link