I am trying to bind date in a model for two cultures (en-US and fr-FR) when the culture is selected en-US. User selects the Item language for creating an entry. For example : Add news in en-US | add news in fr-FR
Model:
[Column(TypeName = "datetime2")]
public DateTime CreatedOn { get; set; }
View:
@Html.TextBox("CreatedOn", Model.CreatedOn, new { @class = "form-control", @id = "datepicker" , @type="text"})
Script for datepicker and parsing:
$(document).ready(function () {
$('#datetimepicker2').datetimepicker({
locale: '@Model.CultureInfoCode',
format: 'L'
}).data("DateTimePicker").date();
//$('#datetimepicker2').moment().format("MM/dd/YYYY");
});
$(document).ready(function () {
$.validator.methods.date = function (value, element) {
moment.locale('@Model.CultureInfoCode'); // CultureInfoCode is the culture of item being entered.
return moment(value);
//parseDate(value, null, "@Model.CultureInfoCode");
// return this.optional(element) || Globalize.parseDate(value) !== null;
}
});
Parsing date seems to work fine also. Model binds all values including date if the culture selected for whole website(en-US ) matches the culture of Item being entered (i.e Add news in English (en-US)). But when I add news in French(fr-FR) and enter date as 26/01/0001, it binds all fields except date and gives value in model as 1/1/0001 and returns in same view displaying error message:
**The value '26/01/0001' is not valid for 'CreatedOn' .**
model.Validate is also false in controller.
Please help. Thanks
Default model binder in asp.net mvc is aware of date localization issues. To make it parse date in specified culture, you need to set CurrentCulture before action method is called. I am aware of few possible ways to do this.
Globalization element
Globalization element can automatically set CurrentCulture to user's OS default. Web.config:
So if user comes from en-US localized OS, date will be parsed in en-US format, from fr-FR localized OS - in fr-FR format, and so on.
Action filters
If you need to set CurrentCulture manually, you can use action filters to do this. More information can be found in this answer. You can use this approach if culture code is specified in url.
Define route:
Define action filter:
Use it per action/controller as attribute or add this to global filters collection in Application_Start():
Model binders
Another way is to re implement model binder for DateTime type. More information here. For example, you have culture field in form. Simplest model binder for this:
Add binder to collection in Application_Start():