2012-03-26 1 views
2

私のフォームにはDateTimeフィールドがあります。何らかの理由で、ModelStateというフォームを送信するたびに、ビューモデルでは必須の属性セットがないのに、私の日付時刻フィールド(PostDateと呼ばれる)が必要であるというメッセージが返されます。DateStateはModelStateで必要ですが、決して

私はそれを理解しようとしているサークルの周りを回っているので、これが起こっている理由を知っていますか?ここで

は、ここでビューモデル

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

「DateTime?」を使用してみましたか? – bevacqua

+0

それは、ありがとう、ニコだった – Chris

答えて

6

ですDateTimeは値型なので無効です。空の値を許可する場合は、null可能なDateTimeを使用する必要があります。

​​
関連する問題