c# - Showing Only Date in Shamsi Calendar -
i using codes show persian date :
this model :
public class paperorder { public int paperorderid { get; set; } [displayname("سفارش دهنده")] public string customername { get; set; } [displayname("گیرنده")] public string receivername { get; set; } [displayname("شماره تماس")] public string tel { get; set; } [displayname(" نوع سفارش")] public string ordertype { get; set; } [displayname("تعداد")] public string count { get; set; } [displayname("وضعیت سفارش")] public orderstatus orderstatus { get; set; } [displayname("تاریخ ارسال سفارش")] [datatype(datatype.date)] [displayformat(dataformatstring = "{0:yyyy-mm-dd}", applyformatineditmode = true)] public datetime? startdate { get; set; } [displayname("تاریخ اتمام سفارش")] [datatype(datatype.date)] [displayformat(dataformatstring = "{0:yyyy-mm-dd}", applyformatineditmode = true)] public datetime? completedate { get; set; } }
and other classes :
public class persianculture : cultureinfo { private readonly system.globalization.calendar cal; private readonly system.globalization.calendar[] optionals; public persianculture() : this("fa-ir", true) { } public persianculture(string culturename, bool useuseroverride) : base(culturename, useuseroverride) { cal = base.optionalcalendars[0]; var optionalcalendars = new list<system.globalization.calendar>(); optionalcalendars.addrange(base.optionalcalendars); optionalcalendars.insert(0, new persiancalendar()); type formattype = typeof(datetimeformatinfo); type calendartype = typeof(system.globalization.calendar); propertyinfo idproperty = calendartype.getproperty("id", bindingflags.instance | bindingflags.nonpublic); fieldinfo optionalcalendarfield = formattype.getfield("optionalcalendars", bindingflags.instance | bindingflags.nonpublic); var newoptionalcalendarids = new int32[optionalcalendars.count]; (int = 0; < newoptionalcalendarids.length; i++) newoptionalcalendarids[i] = (int32)idproperty.getvalue(optionalcalendars[i], null); optionalcalendarfield.setvalue(datetimeformat, newoptionalcalendarids); optionals = optionalcalendars.toarray(); cal = optionals[0]; datetimeformat.calendar = optionals[0]; datetimeformat.monthnames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" }; datetimeformat.monthgenitivenames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" }; datetimeformat.abbreviatedmonthnames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" }; datetimeformat.abbreviatedmonthgenitivenames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" }; datetimeformat.abbreviateddaynames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" }; datetimeformat.shortestdaynames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" }; datetimeformat.daynames = new string[] { "یکشنبه", "دوشنبه", "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" }; datetimeformat.amdesignator = "ق.ظ"; datetimeformat.pmdesignator = "ب.ظ"; datetimeformat.shortdatepattern = "yyyy/mm/dd"; datetimeformat.shorttimepattern = "hh:mm"; datetimeformat.longtimepattern = "hh:mm:ss"; datetimeformat.fulldatetimepattern = "yyyy/mm/dd hh:mm:ss"; } public override system.globalization.calendar calendar { { return cal; } } public override system.globalization.calendar[] optionalcalendars { { return optionals; } } public static datetime persiantogregorianus(datetime fadate) { return new persiancalendar().todatetime(fadate.year, fadate.month, fadate.day, fadate.hour, fadate.minute, fadate.second, fadate.millisecond); } } public class datetimebinder : imodelbinder { public object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext) { var value = bindingcontext.valueprovider.getvalue(bindingcontext.modelname); var date = (datetime)value.convertto(typeof(datetime), cultureinfo.currentculture); date = persianculture.persiantogregorianus(date); return date; } } public class nullabledatetimebinder : imodelbinder { public object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext) { var value = bindingcontext.valueprovider.getvalue(bindingcontext.modelname); if (value != null) { var date = (datetime)value.convertto(typeof(datetime), cultureinfo.currentculture); if (date!= null) date = persianculture.persiantogregorianus(date); return date; } return null; } } public class datetimeactionfilter : actionfilterattribute { public override void onactionexecuted(actionexecutedcontext filtercontext) { base.onactionexecuted(filtercontext); //if (system.threading.thread.currentthread.currentculture.lcid == 1065) system.threading.thread.currentthread.currentculture = system.threading.thread.currentthread.currentuiculture = new persianculture(); } }
global.asax
protected void application_start() { arearegistration.registerallareas(); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); globalfilters.filters.add(new datetimeactionfilter()); modelbinders.binders.add(typeof(datetime), new datetimebinder()); modelbinders.binders.add(typeof(datetime?), new nullabledatetimebinder()); }
part of edit.cshtml
<div class="form-group"> @html.labelfor(model => model.startdate, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.startdate, new { @id = "pcal1", @class = "pdate form-control" }) @html.validationmessagefor(model => model.startdate, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.completedate, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.completedate, new { @id = "pcal2", @class = "pdate form-control" }) @html.validationmessagefor(model => model.completedate, "", new { @class = "text-danger" }) </div> </div>
every thing works fine,but when want show date in application doesn't show properly.
example when want show date in input shows this(look @ zeros):
in view replace code:
@html.textboxfor(model => model.startdate, new { @id = "pcal1", @class = "pdate form-control" })
with following:
@html.textbox("startdate", model.startdate.value.toshortdatestring(), new { @id = "pcal1", @class = "pdate form-control" })
without specifying specfic method convert datetime
value string
, method tostring()
called shows time part too. avoid must specify proper method toshortdatestring()
in case. don't forget check possible null reference too.
Comments
Post a Comment