使用JQuery Datepicker选择的日期在提交时没有反映在MVC3模型中

use*_*878 7 jquery datepicker asp.net-mvc-3

我很难解决这个问题(我确信我在某处犯了一些愚蠢的错误:))有人可以帮忙.

在提交时,我无法在模型中检索使用JQuery datepicker选择的日期.它给我价值'1/1/0001 12:00:00 AM'.我错过了什么吗?

为了解决我的问题,我使用Visual Studio 2010中的模板创建了一个简单的MVC3应用程序.详情如下:

模型:

namespace DatePickerApp.Models
{
    public class DatePickerClass
    {
        public DateTime BirthDate { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

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();
            }
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

cshtml查看:

@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>
Run Code Online (Sandbox Code Playgroud)

rad*_*scu 2

发生这种情况是因为 jquery 中的日期格式不是 C#/VB 中 DateTime 数据的预期格式日期。将 jq 日期格式更改为与 '1/1/0001 12:00:00 AM' 相同

正确的方法可能是:$("#date").datepicker({ dateFormat: 'dd/mm/yy hh:MM:ss' });

PS 这个 '1/1/0001 12:00:00 AM' 是 C# 中的默认日期时间值

检查文档:http://api.jqueryui.com/1.8/datepicker/#option-dateFormat

此链接也可以帮助您实现上午/下午部分: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});  
Run Code Online (Sandbox Code Playgroud)

使用该控制器:

[HttpPost]
    public ActionResult Index(DateTime? date)
    {
        //do something with date
    }
Run Code Online (Sandbox Code Playgroud)

文档中的 javascript 已准备就绪:

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

现在日期将相应填写。