public static string GetRangeErrorMessage(int min, int max, string field) { var attr = new RangeAttribute(min, max); return attr.FormatErrorMessage(field); }
public DateTime? getJoinedDate() { var yearField = Request["JoinedDateYear"]; var monthField = Request["JoinedDateMonth"]; var required = new RequiredAttribute(); if (string.IsNullOrEmpty(yearField)) { ModelState.AddModelError("JoinedDate", required.FormatErrorMessage(WebResources.JoiningDateYear)); } else if (string.IsNullOrEmpty(monthField)) { ModelState.AddModelError("JoinedDate", required.FormatErrorMessage(WebResources.JoiningDateMonth)); } else { var dateRange = new RangeAttribute( typeof(DateTime), MinDate.ToShortDateString(), DateTime.Now.ToShortDateString() ); DateTime? joinedDate = null; try { joinedDate = new DateTime(Convert.ToInt32(yearField), Convert.ToInt32(monthField), 1); } catch (FormatException ex) { ModelState.AddModelError("JoinedDate", ex); } catch (ArgumentOutOfRangeException) { ModelState.AddModelError("JoinedDate", dateRange.FormatErrorMessage(WebResources.JoiningDate)); } catch (ArgumentException ex) { ModelState.AddModelError("JoinedDate", ex); } if (joinedDate != null) { var now = DateTime.Now; if (!dateRange.IsValid(joinedDate.Value)) { ModelState.AddModelError("JoinedDate", dateRange.FormatErrorMessage(WebResources.JoiningDate)); } else { return joinedDate; } } } this.ViewData["JoinedDateYear"] = yearField; this.ViewData["JoinedDateYear"] = monthField; return null; }