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

标签 asp.net-mvc asp.net-mvc-3 modelstate

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

有谁知道为什么会发生这种情况,因为我绕着圈子试图弄清楚。

这是 View 模型

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

这里是 Controller Action

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

这里是 View

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

最佳答案

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

public DateTime? PostDate { get; set; }

关于asp.net-mvc - ModelState 中需要 DateTime,但从未设置为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9866332/

相关文章:

c# - MVC LabelFor 用 CamelCase 替换下划线

c# - Active Directory 登录后,ASP.NET MVC 应用程序重定向到本地主机而不是应用程序 URI

c# - 将 Autofac 与 SignalR 结合使用时出现范围错误

c# - ModelState 有效,但是当我保存数据时,我在有错误的entityValidationErrors 中得到它

asp.net-mvc-3 - MVC 3 中的 ModelState 类是什么?

asp.net - 如何手动调用验证属性? (DataAnnotations 和 ModelState)

asp.net-mvc - jQuery.Load() 不触发 ASP.NET MVC2 中的 Request.IsAjaxRequest

c# - 是什么导致 'Thread was being aborted' 异常随机发生并向浏览器显示 HTTP header 和部分 HTML?

c# - MVC 3 成员资格和授权

asp.net-mvc-3 - MVC3 中 ViewBag 的替代方案