Your SlideShare is downloading. ×
0
DateTime Conversions                    in           C# .NET MVCThis short presentation will cover the basicsof using this...
UiDateTimeModelThe UiDateTimeModel is at the core of everything DateTime. There are a set of propertiesspecifically for us...
UiDateTimeRangeModelClosely related to the UiDateTimeModel and potentially used as extensively throughout manyapplications...
ValidationAttributes    Below you will find the ValidationAttributes which will setup both the Clientside    Unobtrusive v...
ValidationAttributesImplementationUiDateTimeModel     [UiDateTimeFormatDateValidation("LocalDate", ErrorMessageResourceNam...
ValidationAttributesImplementation continued By far the most commonly used attribute that you will implement is the UiDate...
Editor Templates/ Views/Shared/EditorTemplates/Date.cshtmlDateTime.cshtmlDateTimeNoSetTime.cshtmlDate_TopUnit.cshtmlRangeD...
Editor TemplateImplementationViewsSomeFile.cshtml<div class="formrow">@Html.EditorFor(m => m.ActionDate, "DateTimeNoSetTim...
Upcoming SlideShare
Loading in...5
×

C# ASP.Net MVC DateTime Conversions

1,747

Published on

With an enterprise level application that has both users and organizations spread across multiple timezones, the handling of date and time conversions seems to be a universal challenge. While this problem is not unique to C# it just happened that the project I am currently working on is a C# Web Application (ASP.NET, MVC 3).

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,747
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "C# ASP.Net MVC DateTime Conversions"

  1. 1. DateTime Conversions in C# .NET MVCThis short presentation will cover the basicsof using this project for DateTimeconversions in your own solution.This is not in any way meant to be anexhaustive coverage of the extensibilitypoints of C# .NET MVC that are leveragedto make this possible.
  2. 2. UiDateTimeModelThe UiDateTimeModel is at the core of everything DateTime. There are a set of propertiesspecifically for use in the Views and a single property that is primarily used in the Server Side code.Available Properties for use in the Views:Views/FormsDateTimeLocalValue - DateTime?LocalDate -StringLocalTime -StringTimeZoneName -String: Required Property in ALL forms posting a UiDateTimeModelNoSetTime -BoolView Only Properties (String Properties)LocalDateTimeLocalDateTimeAbreviatedMonthNameLocalDateTimeDayOfMonthLocalDateTimeDayWithFullDateFlags:ImplicitlySet -Bool: used to indicate that either the Date or Time of the DateTimeLocalValue was set via the model binding and NOT via a form post or other end user activities.Please note that the UiDateTimeModel requires a TimeZone at the time of instantiation:var model = new UiDateTimeTestModel { UiDateTime = new UiDateTimeModel(timeZone) { } };
  3. 3. UiDateTimeRangeModelClosely related to the UiDateTimeModel and potentially used as extensively throughout manyapplications is the UiDateTimeRangeModel. The two most important (and only) properties ofthis model are the StartDateTime and EndDateTime which are both of typeUiDateTimeModel.Additionally you will want to note that there are three different constructors which improve theusability of the UiDateTimeRangeModel especially when this model must be instantiated formapping purposes.Constructors:UiDateTimeRangeModel(string timeZoneName)UiDateTimeRangeModel(string timeZoneName, DateTime? startDateUtc, DateTime? endDateUtc)UiDateTimeRangeModel(string timeZoneName, DateTime? startDateUtc, DateTime? endDateUtc, boolisAllDay = false)Please note that there is NO default constructor allowing instantiation of this model without aTimeZone.
  4. 4. ValidationAttributes Below you will find the ValidationAttributes which will setup both the Clientside Unobtrusive validation and the Server side validation. They all have very descriptive names. UiDateTimeFormatDateValidation UiDateTimeFormatTimeValidation UiDateTimeGreaterThanDateAttributeOrNullValidation UiDateTimeGreaterThanDateAttributeValidation UiDateTimeGreaterThanTimeAttributeValidation UiDateTimeMaxLimitDateValidation UiDateTimeNotInFutureValidation UiDateTimeNotInPastValidation UiDateTimeRequiredIfAttributeValidation UiDateTimeRequiredIfAttributeValueEqualsValidation UiDateTimeRequiredIfAttributeValueNotEqualsValidation UiDateTimeRequiredIfNotAttributeValidation UiDateTimeRequiredValidation
  5. 5. ValidationAttributesImplementationUiDateTimeModel [UiDateTimeFormatDateValidation("LocalDate", ErrorMessageResourceName = "DateFormatValid", ErrorMessageResourceType = typeof(Resources.Validation))] [UiDateTimeFormatTimeValidation("LocalTime", ErrorMessageResourceName = "TimeFormatValid", ErrorMessageResourceType = typeof(Resources.Validation))] [UiDateTimeMaxLimitDateValidation("LocalDate", 50, ErrorMessageResourceName = "DateMustBeLessThanYearsInFuture", ErrorMessageResourceType = typeof(Resources.Validation))] public class UiDateTimeModel : IComparableCampaignModel.cs [UiDateTimeDisplayAttribute("StartDateTime.LocalDate", "StartDate", typeof(Resources.UI) )] [UiDateTimeDisplayAttribute("EndDateTime.LocalDate", "EndDate", typeof(Resources.UI))] [UiDateTimeNotInPastValidation("StartDateTime.LocalDate", ErrorMessageResourceName = "DateNotInPastValid", ErrorMessageResourceType = typeof(Resources.Validation))] [UiDateTimeGreaterThanDateAttributeOrNullValidation("EndDateTime.LocalDate", "StartDateTime.LocalDate", f alse, ErrorMessageResourceName = "DateMustBeAfterDateValid", ErrorMessageResourceType = typeof(Resources.Validation))] public UiDateTimeRangeModel CampaignDateRange { get; set; } Note: This ValidationAttributes work the same as any other validation attributes that we use on model properties with one important difference. That being the extra parameter that defines the property of the model where a given attribute will be applied.
  6. 6. ValidationAttributesImplementation continued By far the most commonly used attribute that you will implement is the UiDateTimeDisplayAttribute which is used when creating a model property of type UiDateTimeModel along with the set the Display Name for a given UiDateTimeModel property. As seen below we are setting the display name of UiDateTimeModel.LocalDate to be the Resource property “CancellationEffective”. [UiDateTimeDisplayAttribute("LocalDate", "CancellationEffective", type of(Resources.Admin))] public UiDateTimeModel CancellationEffective { get; set; }
  7. 7. Editor Templates/ Views/Shared/EditorTemplates/Date.cshtmlDateTime.cshtmlDateTimeNoSetTime.cshtmlDate_TopUnit.cshtmlRangeDateTimeNoSetTime.cshtmlRangeDateTimeToDateTime.cshtmlRangeDateTimeToTime.cshtmlRangeDateToDate.cshtmlRangeTimeToTime.cshtmlTime.cshtmlTimeNoSetTime.cshtmlUiDateTimeModel.cshtmlUiDateTimeRangeModel.cshtml
  8. 8. Editor TemplateImplementationViewsSomeFile.cshtml<div class="formrow">@Html.EditorFor(m => m.ActionDate, "DateTimeNoSetTime", new { LocalDate = new { @class = "w75" }, LocalTime = new { @class = "w65", disabled="disabeld" } })</div>ViewsActionYetAnotherFile.cshtml@Html.EditorFor(m => m.ScheduledDateTime, "RangeDateTimeNoSetTime", new { StartDateTime_LocalDate = new { @class = "w75", id ="ScheduledAction_Date" }, StartDateTime_LocalTime = new { @class = "w65" } }) Note: In both implementations of the Editor Templates above the third property of the template defines and HTML object that will be used to add additional attributes to the model property defined.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×