i have set culture in Action Filer as
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
where culture = {fr-be} =>French Belgium.
FYI, this action filter sets the culture on user choice.
in one of myAction user is entering Date in format [dd/mm/yyyy] => 26/7/2011. Action is as follows
public ActionResult RequestVacation(VacationRequest model)
{
if(ModelState.IsValid)
{
....
when i dubug the code model.VacationDate contains 01/01/0001 ; although it should be 7/26/2011 whereas Form[VacationDate] contains 26/07/2011 [which is in Fr-BE formate] And ModelState.IsValid is false; although it should be true, as date is correct in fr-be format. when i furtur dig out but checking locals in visual studio i found
this.ModelState[1].Culture = {en-US}
whereas i had already set culture value using actionFilter, as stated above. My question is how can i set this.ModelState.Culture = {fr-be}?
In response to my above question i had solved it in this way
if (ModelState.Keys.Contains("VactionDate"))
{
ModelState err = ModelState["VactionDate"];
if (!err.Value.Culture.Equals(Thread.CurrentThread.CurrentCulture))
{
try
{
DateTime dt = Convert.ToDateTime(err.Value.AttemptedValue, Thread.CurrentThread.CurrentCulture.DateTimeFormat);
model.VactionDate = dt;
ModelState.Remove("VactionDate");
}
catch
{
}
}
}
i know this is not a good solution. but i m still looking for some way to change, before validation occurs,
ModelState[n].Value.Culture = {en-US}
To
ModelState[n].Value.Culture = {fr-BE}
where {fr-BE} is my required Culture, for dateTime to be parsed. so i m still looking for someone to find out a good solution for this.