I don't want the error message to be "Field <field> must be a date" if I select a date in the format "yyyy / MM / dd" in the en-AU locale
In the model, I am using the code below:
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(InformationResources))] [DataType(DataType.Date, ErrorMessageResourceName = "DateInvalid", ErrorMessageResourceType = typeof(InformationResources))] [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)] [FutureDate(ErrorMessageResourceName = "DateInPast", ErrorMessageResourceType = typeof(InformationResources))] public DateTime? FirstTrade { get; set; }
In view, I am using below code:
<div> @Html.TextBoxFor(m => m.FirstTrade, new { @Value = Convert.ToDateTime(Model.FirstTrade).ToString("yyyy/MM/dd"),@class = "form-control input-sm", @type = "date", @max = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd") }) @Html.ValidationMessageFor(model => model.FirstTrade) </div>
Now if I select data using date picker in "yyyy / MM / dd" format, I get an error like "FirstTrade field must be date". One more thing I use en-Au locale. Please let me know how to remove the error message. If I choose a date in the format "yyyy / MM / dd".
Thanks in advance.
+1
source share
1 answer
You should use the format you like when displaying the date value.
Try the following:
@Html.TextBoxFor(m => m.FirstTrade, "{0:yyyy/MM/dd}", new { @Value = Convert.ToDateTime(Model.FirstTrade).ToString("yyyy/MM/dd"),@class = "form-control input-sm", @type = "date", @max = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd") })
This is what my model and view looks like:
Model:
public class Order { public int OrderId { get; set; } [Display(Name = "Incorporation date")] public DateTime? IncorporationDate { get; set; } [Display(Name = "Capital payment date")] public DateTime? CapitalPaymentDate { get; set; } }
View:
@Html.LabelFor(model => model.IncorporationDate) <div class="input-prepend"> <span class="add-on"><i class="icon-briefcase"></i></span> @Html.TextBoxFor(model => model.IncorporationDate, "{0:dd.MM.yyyy}", new { @class = "span3 date", @data_date_format = "dd.mm.yyyy", @placeholder = "Incorporation date" }) </div> @Html.LabelFor(model => model.CapitalPaymentDate) <div class="input-prepend"> <span class="add-on"><i class="icon-briefcase"></i></span> @Html.TextBoxFor(model => model.CapitalPaymentDate, "{0:dd.MM.yyyy}", new { @class = "span3 date", @data_date_format = "dd.mm.yyyy", @placeholder = "Capital payment date" }) </div>
Edit:
Try setting the globalization tag in system.web to the culture you want:
<system.web> <globalization uiCulture="nb-NO" culture="nb-NO"/> </system.web>
+2
source share