Asp.net mvc ModelState中需要DateTime,但从未设置为

Asp.net mvc ModelState中需要DateTime,但从未设置为,asp.net-mvc,asp.net-mvc-3,modelstate,Asp.net Mvc,Asp.net Mvc 3,Modelstate,我的表单中有一个DateTime字段。无论出于何种原因,每次我提交表单ModelState时,都会返回一条无效消息,表明我的日期时间字段(称为PostDate)是必需的,即使在视图模型中我没有设置必需的属性 有人知道为什么会发生这样的事吗?因为我在兜圈子,想弄明白 这是视图模型 public class BlogViewModel { [Required] public string Title { get; set; }

我的表单中有一个DateTime字段。无论出于何种原因,每次我提交表单ModelState时,都会返回一条无效消息,表明我的日期时间字段(称为PostDate)是必需的,即使在视图模型中我没有设置必需的属性

有人知道为什么会发生这样的事吗?因为我在兜圈子,想弄明白

这是视图模型

public class BlogViewModel
        {
            [Required]
            public string Title { get; set; }
            [Required]
            public string Content { get; set; }
            public bool Published { get; set; }
            public DateTime PostDate { get; set; }
        }
下面是控制器的操作

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

            //
            // POST: /Admin/Blog/Create

            [HttpPost]
            [ValidateInput(false)]
            public ActionResult Create(BlogViewModel model)
            {
                if(ModelState.IsValid)
                {
                    model.Content = HtmlSanitizer.SanitizeHtml(model.Content);
                    Services.BlogService.CreateBlogPost(model.Title, model.Content, User.Identity.Name);
                    return RedirectToAction("Index");
                }
                return View(model);
            }
这里是风景

    @using Payntbrush.Infrastructure.Mvc.Extensions
    @model Payntbrush.Presentation.Demo.MVC3.Areas.Admin.Models.BlogViewModel

    @Html.Resource(Html.ScriptTag("Areas/Admin/js/plugins/wysiwyg/jquery.wysiwyg.js"), ResourceType.Js)


    <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>

    @using (Html.BeginForm((string)ViewBag.Action, "Blog", FormMethod.Post, new { Id = "BlogEditor" }))
    {
        @Html.ValidationSummary(true)

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

            <div class="editor-label">
                @Html.LabelFor(model => model.Content)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model.Content, new { @class = "wysiwyg blog-editor" })
                @Html.ValidationMessageFor(model => model.Content)
            </div>

            <div class="editor-label">
                Time of post (Only set this if you want to make a post appear to have been published at a different date.)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.PostDate, new{@class="datetimepicker"})
                @Html.ValidationMessageFor(model => model.PostDate)
            </div>

            <div class="editor-label">
                Published (Check to make this post live on the site)
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(model => model.Published)
                @Html.ValidationMessageFor(model => model.Published)
            </div>

            <p>
                <input class="large awesome" type="submit" value="Create" />
                @Html.ActionLink("Cancel", "Index", "Blog", null, new { @class = "large awesome cancel-button" })
            </p>
    }

    <div>

    </div>
@使用Payntbrush.Infrastructure.Mvc.Extensions
@模型Payntbrush.Presentation.Demo.MVC3.Areas.Admin.Models.BlogViewModel
@Resource(Html.ScriptTag(“Areas/Admin/js/plugins/wysiwyg/jquery.wysiwyg.js”)、ResourceType.js)
@使用(Html.BeginForm((string)ViewBag.Action,“Blog”,FormMethod.Post,new{Id=“blogeeditor”}))
{
@Html.ValidationSummary(true)
@LabelFor(model=>model.Title)
@EditorFor(model=>model.Title)
@Html.ValidationMessageFor(model=>model.Title)
@LabelFor(model=>model.Content)
@Html.TextAreaFor(model=>model.Content,新的{@class=“wysiwyg博客编辑器”})
@Html.ValidationMessageFor(model=>model.Content)
发布时间(仅当您希望使文章显示为在不同日期发布时才设置此时间。)
@Html.TextBoxFor(model=>model.PostDate,新的{@class=“datetimepicker”})
@Html.ValidationMessageFor(model=>model.PostDate)
已发布(选中此项可使此帖子在网站上直播)
@CheckBoxFor(model=>model.Published)
@Html.ValidationMessageFor(model=>model.Published)

@ActionLink(“取消”,“索引”,“博客”,空,新的{@class=“大取消按钮”})

}
如果将相应字段留空,则模型将无效,因为DateTime是一种值类型。如果要允许空值,则应使用可为空的日期时间:

public DateTime? PostDate { get; set; }

你试过使用
DateTime吗?
来代替它吗?就是这样,谢谢Nico