2013-08-04 113 views
0

我有模式已經得到了,因爲其他兩種型號在網站上用戶可以看到說明和用戶可以發表評論:禁用驗證

public class NoteAndCommentViewModel 
{ 
    public Note Note { get; set; } 
    public Comment Comment { get; set; } 
} 

public class Note 
{ 
    [Required] 
    public string Title { get; set; } 

    [Required] 
    public string Summary { get; set; } 
} 

public class Comment 
{ 
    [Required] 
    public string Author { get; set; } 

    [Required] 
    public string Content { get; set; } 
} 

我怎樣才能NoteAndCommentViewModel禁用驗證爲所有屬性註釋?因爲現在在動作控制器我的模型始終是無效的,因爲提交表格後,我回到評價模型,注意模型爲空:

[HttpPost] 
public ActionResult Create(NoteAndCommentViewModel noteAndCommentViewModel) 
{    
    if (ModelState.IsValid) 
    { 
     //..... 
    } 

    //..... 
} 
+0

'如果(ModelState.ContainsKey( 「屬性名」)) 的ModelState [ 「屬性名」] Errors.Clear();' –

回答

0

這個類必須是在模型文件夾視圖模型文件是在另一個文件夾中您可以添加所有這些特性,這需要驗證

public class NoteAndCommentViewModel 
{ 

    [Required] 
    public string Title { get; set; } 

    [Required] 
    public string Summary { get; set; } 

    [Required] 
    public string Author { get; set; } 

    [Required] 
    public string Content { get; set; } 
}