Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date selected using JQuery Datepicker is not reflecting in MVC3 model on submit

I had very tough time to resolve this (and I am sure I am doing some silly mistake somewhere :)) Could somebody please help.

I am not able to retrieve the date selected using JQuery datepicker in the model while submitting. It give me value as '1/1/0001 12:00:00 AM'. Am I missing something?

To isolate my problem I created a simple MVC3 application using a template from Visual Studio 2010. Details as follows:

Model:

namespace DatePickerApp.Models
{
    public class DatePickerClass
    {
        public DateTime BirthDate { get; set; }
    }
}

Controller:

namespace DatePickerApp.Controllers
{
    public class BirthDateController : Controller
    {

        //
        // GET: /BirthDate/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /BirthDate/Create

        [HttpPost]
        public ActionResult Create(DatePickerApp.Models.DatePickerClass DatePickerClass)
        {
            try
            {
                // TODO: Add insert logic here

                return View();
            }
            catch
            {
                return View();
            }
        }


    }
}

cshtml View:

@model DatePickerApp.Models.DatePickerClass

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />  
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>  
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>  
<link rel="stylesheet" href="/resources/demos/style.css" />

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {
        $("#date").datepicker({ dateFormat: 'dd/mm/yy' });
    }); 
</script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>DatePickerClass</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.BirthDate)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.BirthDate, new { id = "date" })
            @Html.ValidationMessageFor(model => model.BirthDate)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
like image 863
user2163878 Avatar asked Mar 13 '13 05:03

user2163878


1 Answers

This happens because your date format from jquery is not the expected format date by DateTime data in C#/VB. Change jq date format to be the same as '1/1/0001 12:00:00 AM'

the correct way may be : $("#date").datepicker({ dateFormat: 'dd/mm/yy hh:MM:ss' });

P.S. this '1/1/0001 12:00:00 AM' is the default datetime value in C#

Check documentation: http://api.jqueryui.com/1.8/datepicker/#option-dateFormat

Also this link may help you to achieve am/pm part : http://derekallard.com/blog/post/adding-time-to-jquery-ui-datepicker/

date_obj = new Date();
date_obj_hours = date_obj.getHours();
date_obj_mins = date_obj.getMinutes();

if (date_obj_mins < 10) { date_obj_mins = "0" + date_obj_mins; }

if (date_obj_hours > 11) {
    date_obj_hours = date_obj_hours - 12;
    date_obj_am_pm = " PM";
} else {
    date_obj_am_pm = " AM";
}

date_obj_time = "'"+date_obj_hours+":"+date_obj_mins+date_obj_am_pm+"'";

$("#entry_date").datepicker({dateFormat: $.datepicker.W3C + date_obj_time});  

Use this controller:

[HttpPost]
    public ActionResult Index(DateTime? date)
    {
        //do something with date
    }

and this javascript in document ready:

$("[name=date]").datepicker({ dateFormat: "m/dd/yy" });

now date will be filled accordingly.

like image 59
radu florescu Avatar answered Oct 21 '22 11:10

radu florescu