Geeks With Blogs
David Carter Software development experiences


As was mentioned in a previous blog I am building a web page that allows the user to select dates in a calendar and then shows the dates in an unordered list. The problem now is that those dates need to be sent to the server on page submit so that they can be saved to the database. If I was storing the dates in an input element, say a textbox, that wouldn't be an issue but because they are in an html element whose contents are not posted to the server an alternative strategy needs to be developed.


The approach that I took to solve this problem is as follows:

1. Place a hidden input field on the form

<input id="hiddenDates" name="hiddenDates" type="hidden" value="" />

ASP.Net MVC has an Html helper with a method called Hidden() that will do this for you @Html.Hidden("hiddenDates").

2. Copy the values from the html element to the hidden input field before submitting the form

The following javascript is added to the page:

       $(function () {

         $('#formCreate').submit(function () {


           function PopulateHiddenDates() {
         var dateValues = '';
         $($('#dateList').children('li')).each(function(index) {
            dateValues += $(this).attr("id") + ",";


I'm using jQuery to bind to the form submit event so that my method to populate the hidden field gets called before the form is submitted. The dateList element is an unordered list and by using the jQuery each function I can itterate through all the <li> items that it contains, get each items id attribute (to which I have assigned the value of the date in millisecs) and write them to the hidden field as a comma delimited string.

3. Process the dates on the server

        public ActionResult Create(string hiddenDates, string utcOffset)

           List<DateTime> dates = GetDates(hiddenDates, utcOffset);

        private List<DateTime> GetDates(string hiddenDates, int utcOffset)
            List<DateTime> dates = new List<DateTime>();

            var values = hiddenDates.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);

            foreach (var item in values)
                DateTime newDate = new DateTime(1970, 1, 1).AddMilliseconds(double.Parse(item)).AddMinutes(utcOffset*-1);

            return dates;

By declaring a parameter with the same name as the hidden field ASP.Net will take care of finding the corresponding entry in the form collection posted back to the server and binding it to the hiddenDates parameter! Excellent! I now have my dates the user selected and I can save them to the database. I have also used the same technique to pass back a utcOffset so that I know what timezone the user is in and I can show the dates correctly to users in other timezones if necessary (this isn't strictly necessary at the moment but I plan to introduce times later),

Saving multiple dates from an unordered list - DONE!

Site Meter

Posted on Friday, January 7, 2011 3:33 PM ASP.Net MVC | Back to top

Comments on this post: ASP.Net MVC - how to post values to the server that are not in an input element

# re: ASP.Net MVC - how to post values to the server that are not in an input element
Requesting Gravatar...
You should checkout KnockoutJS ( This would be a great scenario for posting your data back as JSON.
Left by Ryan on Jan 08, 2011 4:30 PM

# re: ASP.Net MVC - how to post values to the server that are not in an input element
Requesting Gravatar...
Man never made any material as resilient as the human spirit.
Left by Halloween on Aug 31, 2011 6:09 PM

# Simple and effective - perfect!
Requesting Gravatar...
These two posts helped me solve the exact same problem much easire than If I'd had to figure it out from scratch (new to MVC).
Thanks a lot.
Left by Peter H on Aug 10, 2015 12:19 AM

Your comment:
 (will show your gravatar)

Copyright © David Carter | Powered by: