2012-04-06 60 views
5

我在那裏我收集筆記數據輸入字段。每個音符都需要音符數據元素。這是我的模型:asp.net的MVC TextAreaFor是沒有得到證實爲必填字段

public interface INoteDataEntryViewModel : IMobilePageDataContract 
{ 
    int CourseId { get; set; } 

    [Required(ErrorMessage = @"Note is required")] 
    String Note { get; set; } 

    [DisplayName(@"Note Date")] 
    DateTime NoteDate { get; set; } 
} 

您可以看到我具有Note屬性的Required屬性。

我用剃刀來顯示數據錄入表單元素:當我使用

<div data-role="fieldcontain"> 
    @Html.LabelFor(m => m.Note) 
    @Html.TextAreaFor(m => m.Note) 
    @Html.ValidationMessageFor(m => m.Note) 
</div> 

「@ Html.TextAreaFor」再有就是要對所需字段沒有驗證,我可以提交表單。但是,如果我更改爲「@ Html.TextBoxFor」,那麼對所需字段進行驗證,我無法提交表單。關於爲什麼TextAreaFor驗證失敗的任何想法?我使用不顯眼的ajax,我是jQueryMobile。

感謝您的幫助。

回答

7

客戶端驗證沒有爲Html.TextAreaFor()輔助工作,這裏是related issue reported on Codeplex

要使其工作,你必須裝飾與[DataType(DataType.MultilineText)]屬性「註釋」屬性。並且在該視圖中,使用Html.EditorFor()幫手而不是Html.TextAreaFor()輔助方法。

更新型號:

public interface INoteDataEntryViewModel : IMobilePageDataContract 
{ 
    int CourseId { get; set; } 

    [Required(ErrorMessage = @"Note is required")] 
    [DataType(DataType.MultilineText)] 
    String Note { get; set; } 

    [DisplayName(@"Note Date")]  
    DateTime NoteDate { get; set; } 
} 

查看:

<div data-role="fieldcontain"> 
    @Html.LabelFor(m => m.Note) 
    @Html.EditorFor(m => m.Note) 
    @Html.ValidationMessageFor(m => m.Note) 
</div> 
+0

+1出色的工作和周圍explantion – Liam 2012-11-06 13:16:33