2012-04-18 150 views
1

我有以下視圖,該視圖無法驗證Title和NewsContent。標題驗證有效,但不是NewsContent。我如何解決它。MVC3驗證問題

@model Foo.NewsViewModel 
@{ 
    ViewBag.Title = "Create"; 
} 



@using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <div> 
     <fieldset> 
      <legend>Category Information</legend> 
      <div class="editor-label"> 
       @Html.LabelFor(m => m.News.Title) 
      </div> 

      <div class="editor-field"> 
       @Html.TextBoxFor(m => m.News.Title) 
       @Html.ValidationMessageFor(m => m.News.Title) 
      </div> 
       <div class="editor-label"> 
       @Html.LabelFor(m => m.News.NewsContent) 
      </div> 

      <div class="editor-field" id="container"> 
       @Html.TextAreaFor(m => m.News.NewsContent) 
       @Html.ValidationMessageFor(m => m.News.NewsContent) 
      </div> 
      <div class="editor-label"> 
       @Html.LabelFor(m => m.News.Thumbnail) 
      </div> 

      <div class="editor-field"> 
       <input type="file" name="files" id="thumbnail" /> 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(m => m.News.Image) 
      </div> 

      <div class="editor-field"> 
       <input type="file" name="files" id="original" /> 
      </div> 

      <div class="editor-label"> 
       @Html.Label("SelectedCategoryId") 
      </div> 

      <div class="editor-field"> 
       @Html.DropDownListFor(m => m.SelectedCategoryId, Model.Categories) 
      </div> 

      <div class="editor-label"> 
       Publish 
      </div> 

      <div class="editor-field"> 
       @Html.CheckBoxFor(m => m.News.Published, new { @checked = "checked" }) 
      </div> 

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

這裏是模型|:

public class News : IStorable 
    { 
     [Required] 
     [Display(Name = "Title")] 
     public virtual string Title { get; set; } 

     [Required] 
     [Display(Name = "Content")] 
     public virtual string NewsContent { set; get; } 
...... 
+0

像FCKEditor的等它只是textarea的現在BT我會添加所見即所得 – 2012-04-18 15:00:03

+0

新聞內容富文本區域? – DarthVader 2012-04-18 15:01:18

+0

實體框架「的味道」你用它 – NSGaga 2012-04-18 15:08:31

回答

3

問題:題目驗證工作,但不NewsContent。

驗證是不行的,因爲使用Html.TextAreaFor()輔助渲染「NewsContent」屬性,

這裏是使其工作代碼:

更改模型爲:

用[DataType]屬性裝飾'NewsContent'屬性並將數據類型設置爲'MultilineText'。這將表明該屬性的編輯器應該是多行文本輸入。

public class News : IStorable 
{ 
    [Required] 
    [Display(Name = "Title")] 
    public virtual string Title { get; set; } 

    [Required()] 
    [Display(Name = "Content")] 
    [DataType(DataType.MultilineText)] 
    public virtual string NewsContent { set; get; } 
    //.... 
} 

在視圖使用Html.EditorFor()輔助代替Html.TextAreaFor()的 'News.NewsContent' 屬性。

//.... 
<div class="editor-label"> 
    @Html.LabelFor(m => m.News.NewsContent) 
</div> 

<div class="editor-field" id="container"> 

    @*@Html.TextAreaFor(m => m.News.NewsContent)*@ 

    @Html.EditorFor(m => m.News.NewsContent) 
    @Html.ValidationMessageFor(m => m.News.NewsContent) 
</div> 
//.... 
+0

工作。真棒。謝謝。 – DarthVader 2012-04-18 18:44:36