i am trying to implement a Custom Model Binding for DateTime field
the binding works well when language is ennglish but not when i switch it to Arabic.
public class MyUserModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var form = controllerContext.HttpContext.Request.Form;
int day, month, year;
string[] dayMonthYear = form["EnterTime"].Split('/');
int.TryParse(dayMonthYear[0], out day);
int.TryParse(dayMonthYear[1], out month);
int.TryParse(dayMonthYear[2], out year);
var date = string.Format("{0}/{1}/{2}", month, day, year);
DateTime dateOfBirth;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
DateTimeStyles styles = DateTimeStyles.None;
if (DateTime.TryParse(date, culture, styles, out dateOfBirth))
{
return dateOfBirth;
}
else
{
bindingContext.ModelState.AddModelError("DateOfBirth",
"Date was not recognised");
return null;
}
}
}
public class MyUserModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var form = controllerContext.HttpContext.Request.Form;
int day, month, year;
string[] dayMonthYear = form["EnterTime"].Split('/');
int.TryParse(dayMonthYear[0], out day);
int.TryParse(dayMonthYear[1], out month);
int.TryParse(dayMonthYear[2], out year);
DateTime dateOfBirth;
var date = string.Format("{0}/{1}/{2}", month, day, year);
dateOfBirth = Convert.ToDateTime(date);
return dateOfBirth;
}
}
and i added attribute above DateTime field
public class Attendance
{
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime EnterTime{ set; get; }
}
Sincerely I don't understand what is the purpose of your custom model binder...so maybe my advices are not appropriate. For sure the way you are handling date doesnt take account globalization...
There are a lot of already working way to handle dates, that works WITH MOST OF cultures...probably you are on a wrong path...trying to solve a globalization problem with a custom model binder instead of setting asp.net globalization properly...Let me know
Sincerely I don't understand what is the purpose of your custom model binder...so maybe my advices are not appropriate. For sure the way you are handling date doesnt take account globalization...
There are a lot of already working way to handle dates, that works WITH MOST OF cultures...probably you are on a wrong path...trying to solve a globalization problem with a custom model binder instead of setting asp.net globalization properly...Let me know
yea it works when language is english but does not work when language is arabic..
the reson why i choose to make custom model binder to DateTime fields is beacause when language not english i got DateTime field binds to not correct value
i am trying to focus on resolution to this problem
well just read the link I passed that explain how to set up globalization both on client side and on server side. YOU DON'T NEED A CUSTOM BINDER, but just setting properly globalization
AnasTeinah
Member
157 Points
104 Posts
Custom Model Binding (DateTime)
May 22, 2012 11:14 AM|LINK
hi
i am trying to implement a Custom Model Binding for DateTime field
the binding works well when language is ennglish but not when i switch it to Arabic.
public class MyUserModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var form = controllerContext.HttpContext.Request.Form; int day, month, year; string[] dayMonthYear = form["EnterTime"].Split('/'); int.TryParse(dayMonthYear[0], out day); int.TryParse(dayMonthYear[1], out month); int.TryParse(dayMonthYear[2], out year); var date = string.Format("{0}/{1}/{2}", month, day, year); DateTime dateOfBirth; CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US"); DateTimeStyles styles = DateTimeStyles.None; if (DateTime.TryParse(date, culture, styles, out dateOfBirth)) { return dateOfBirth; } else { bindingContext.ModelState.AddModelError("DateOfBirth", "Date was not recognised"); return null; } } }thanks in advance
blurearc
Contributor
3710 Points
692 Posts
Re: Custom Model Binding (DateTime)
May 22, 2012 11:23 AM|LINK
check these links .. they might help
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.longdatepattern.aspx
http://forums.asp.net/t/1547049.aspx/1
"There are NO shortcuts for success, better stop taking one."
Ravi Kant Srivastava
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Custom Model Binding (DateTime)
May 22, 2012 11:47 AM|LINK
don't use DateTime.Parse it doesn't take into account culture. Use Convert.ToDateTime(x, CultureInfo.CurrentCulture).
Then be sure you have set properly asp.net globalization.
If you need to understand better globalization issues give a look to this post of my blog:
http://www.dotnet-programming.com/post/2011/12/14/Globalization-Validation-and-DateNumber-Formats-in-AspNet-MVC.aspx
Mvc Controls Toolkit | Data Moving Plug-in Videos
AnasTeinah
Member
157 Points
104 Posts
Re: Custom Model Binding (DateTime)
May 22, 2012 12:02 PM|LINK
thanks for replay
i have tried it, but i got "String was not recognized as a valid DateTime" exception when i switch to another language.
i will look at provided link and replay later
AnasTeinah
Member
157 Points
104 Posts
Re: Custom Model Binding (DateTime)
May 23, 2012 04:49 AM|LINK
UP
AnasTeinah
Member
157 Points
104 Posts
Re: Custom Model Binding (DateTime)
May 23, 2012 06:10 AM|LINK
here is the new code
public class MyUserModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var form = controllerContext.HttpContext.Request.Form; int day, month, year; string[] dayMonthYear = form["EnterTime"].Split('/'); int.TryParse(dayMonthYear[0], out day); int.TryParse(dayMonthYear[1], out month); int.TryParse(dayMonthYear[2], out year); DateTime dateOfBirth; var date = string.Format("{0}/{1}/{2}", month, day, year); dateOfBirth = Convert.ToDateTime(date); return dateOfBirth; } }and i added attribute above DateTime field
public class Attendance { [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime EnterTime{ set; get; } }francesco ab...
All-Star
20912 Points
3279 Posts
Re: Custom Model Binding (DateTime)
May 23, 2012 07:02 AM|LINK
Does this work?
Sincerely I don't understand what is the purpose of your custom model binder...so maybe my advices are not appropriate. For sure the way you are handling date doesnt take account globalization...
There are a lot of already working way to handle dates, that works WITH MOST OF cultures...probably you are on a wrong path...trying to solve a globalization problem with a custom model binder instead of setting asp.net globalization properly...Let me know
Mvc Controls Toolkit | Data Moving Plug-in Videos
AnasTeinah
Member
157 Points
104 Posts
Re: Custom Model Binding (DateTime)
May 23, 2012 07:13 AM|LINK
yea it works when language is english but does not work when language is arabic..
the reson why i choose to make custom model binder to DateTime fields is beacause when language not english i got DateTime field binds to not correct value
i am trying to focus on resolution to this problem
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Custom Model Binding (DateTime)
May 23, 2012 11:53 AM|LINK
well just read the link I passed that explain how to set up globalization both on client side and on server side. YOU DON'T NEED A CUSTOM BINDER, but just setting properly globalization
Mvc Controls Toolkit | Data Moving Plug-in Videos
AnasTeinah
Member
157 Points
104 Posts
Re: Custom Model Binding (DateTime)
Jun 07, 2012 01:56 PM|LINK