获取日期时间总是空回到MVC3中的控制器 [英] Getting date time always null back to controller in MVC3

查看:139
本文介绍了获取日期时间总是空回到MVC3中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC3和jQuery插件datepicker.

Using MVC3 and jQuery plugin datepicker.

我可以看到日历,并且可以在字段中设置日期.但是,当我将模型发布回控制器时,我总是会得到该属性的空值.并将该特定日期显示为无效日期.

I can see calender and I am able to set the date to the field. But when I post model back to controller, I am always getting null value for the property. And it is displaying that particular date as a not valid date.

这是我的模特.

[DataType(DataType.DateTime)]
        [DisplayName("PostTime")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? PostTime { get; set; }

也是我的观点

    @using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
    <legend>BlogPost</legend>

    <div class="editor-label">
    @Html.LabelFor(model => model.PostTitle)
    </div>
    <div class="editor-field">
    @Html.EditorFor(model => model.PostTitle)
    @Html.ValidationMessageFor(model => model.PostTitle)
    </div>

    <div class="editor-label">
    @Html.LabelFor(model => model.PostTime)
    </div>
    <div class="editor-field">
    <span class="datepicker">
    @Html.EditorFor(model => model.PostTime)
    @Html.ValidationMessageFor(model => model.PostTime)
    </span>
    </div>


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

    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#PostTime").datepicker({
                    showOn: 'both',
                    buttonImage: "/content/img/calendar_icon.png"
                });
            });
        </script>

控制器是

[HttpPost]
public ActionResult Create(BlogPost blogpost)
{
    if (ModelState.IsValid)
    {
        var db = new BlogPostContext();
        db.Add(blogpost);
        return RedirectToAction("Index");
    }
    return View(blogpost);
}

有人可以让我缺少什么吗?

Can somebody what I am missing.

谢谢.

推荐答案

只要收到错误消息说它不是有效日期,就需要指定日期选择器的格式:

As far as getting an error saying it isn't a valid date, you'll need to specify the format of the datepicker:

        $(document).ready(function () {
            $("#PostTime").datepicker({
                showOn: 'both',
                dateFormat: 'dd/mm/yy',
                buttonImage: "/content/img/calendar_icon.png"
            });
        });

这篇关于获取日期时间总是空回到MVC3中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆