I have a view with datepicker control on it and couple of partial views on the page. The date is set to today’s date by default in the ViewData of the controller and is used to display when the page loads. When the user changes the date in the date picker, how do I pass the selected value from Jquery variable to Controller’s ViewData and update all the partial views?
Share
The date picker control is just an HTML element that has some associated javascript to help the user select a date. It’s completely client side which means that it runs on the browser (e.g. IE, Firefox, Chrome). ViewData is a temporary store that can be populated by your server side code (e.g. C#) in the controller action, and then used within a view to generate the HTML response that will get sent to the client browser. You cannot therefore pass a jQuery variable to the ViewData, it just doesn’t make sense.
If you want to update the parts of the page that was generated by your partials, you have a couple of options.
(a) do a postback passing the datetime back to a controller action and refresh the entire page (pretty simple but might not give a good user experience depending on what you are trying to do).
(b) use Ajax to update the sections of the page that you want to (more involved but can give a better user experience).