This project is read-only.

jQuery date picker defaults

Nov 8, 2012 at 4:21 PM

I am using DateTimeFor helper and jquery datepicker for showing calendar. I managed to set all needed CalendarOptions for one helper instance, but I couldn't force this helper to pick up default settings for all instances of jQuery datepicker. I don't want to set same CalendarOptions on every page that uses datepicker (e.g. calendar button image) because that would produce unnecessary duplications.

To be more specific, in my _layout.cshtml I have this snippet:

$(document).ready(function () {
            $.datepicker.setDefaults({ buttonImage: "/images/calendar.gif", buttonImageOnly: true, showOn: "button" });


On my page, I have:

@{var d = Html.DateTimeFor(m => m.datevisited, true); }               
@d.DateCalendar(inLine: false, calendarOptions: new CalendarOptions { ShowButtonPanel = true, ShowOn = CalendarOptionsShowOn.button })

If I omit calendarOptions, datepicker doesn't pick up defaults I specified in master layout page.

Is there a solution to my problem?

Nov 9, 2012 at 2:36 PM

jquery defaults doesnt work, because you are setting evrything on the server, and a msimatch between server and client settings might cause client defaults are ignored.

HOWEVER, you can handle defaults on the server side, in a very simple way:


  1. put a CalendarOptions instance with all defaults you want in some static variable in your project. You can also use a dictionary, where to each string you associate different default
  2. instead of creating directly an instance of CalendarOptions with new, define a static function that creates the CalendarOptions instance and then copy all defaults settings in it. Then you can operate on this object by changing just the propertied that needs to be different from your defaults.