2013-03-29 86 views
0

我有下面的視圖模型,它們用於表示問題調查,但它們被構造成更扁平化的網格以容納默認模型綁定器。MVC中沒有數據註釋的自定義模型和客戶端驗證

// Main ViewModel for the Question View 
public class SurveyRowList 
{ 
    ... 

    public IList<SurveyRow> SurveyRowList { get; set; } 
} 

public class SurveyRow 
{ 
    public int QuestionId { get; set; } 
    public int? ParentQuestionId { get; set; } 
    public int SurveyId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string HelpInformation { get; set; } 
    public int RenderOrder { get; set; } 

    public SurveyRowType RowType { get; set; } 

    // Collection of the same answer control, 1 or more times 
    // for each line number 
    public IList<AnswerControl> AnswerControls { get; set; } 
} 

public enum SurveyRowType 
{ 
    QuestionGroup = 1, 
    Question = 2, 
    AnswerRow = 3 
} 

public class AnswerControl 
{ 
    public int Id { get; set; } 
    public int QuestionId { get; set; } 

    // a reference to the database record answer id 
    public int SurveyAnswerId { get; set; } 

    // control type of checkbox, dropdown, input, dropdown-additional-textbox, checkbox-group 
    public ControlType ControlType { get; set; } 

    // used to specify getting particular backing data for dropdown and checkbox-group 
    public ControlSpecificType ControlSpecificType { get; set; } 

    public string Description { get; set; } 
    public string HelpInformation { get; set; } 
    public int RenderOrder { get; set; } 
    public bool InLine { get; set; } 

    public int LineNumber { get; set; } 

    public AnswerControlValueType Value { get; set; } 

} 

public class AnswerControlValueType 
{ 
    // Default string backing value when possible 
    public string Value { get; set; } 

    // AnswerCheckBox 
    public bool CheckValue { get; set; } 

    // AnswerCheckBoxListModal 
    public string ModalName { get; set; } 

    // AnswerMultiSelectListValue 
    public int[] ListValues { get; set; } 
    // making the options list setter public so that this data can be re-attached after model binding 
    public IEnumerable<SelectListItem> ListOptions { get; set; } 

    // AnswerImageValue 
    public HttpPostedFileBase Image { get; set; } 

    // AnswerSelectListAdditionalValue 
    public string AdditionalInformation { get; set; } 
} 

每個SurveyRow就像一排表。只有SurveyRowType.AnswerRow實際上使用AnswerControls列表。當通過它們的類型和順序號呈現他們的訂購

實施例可以看出在此圖像中: enter image description here

圖像只顯示一些簡單的例子,並且可以有每頁1-10行到最大值爲100,但我也添加了一些我想要應用的驗證規則的解釋。還有更多,但這些只是一些例子。

我的問題是,我想支持這個更復雜的驗證,但所有的規則和錯誤文本都存儲在數據庫中,1.因爲用戶配置,2.因爲錯誤文本的現有本地化,以支持幾種語言。

我在尋找人們可能必須能夠支持的任何建議。

我已經看到Fluent Validation之類的東西,但我還沒有深入研究,但到目前爲止,我看不到任何示例,特別是不會在模型上使用數據批註..並且還需要EnableIf或DisabledIf或EnabledIf樣式驗證適用於稍微複雜一些對象的規則。

+0

我只是推薦fluentvalidation,但已經有了,其他選項可以是企業庫驗證塊。 – Marco

+0

到目前爲止,它看起來像都依賴模型屬性上的數據註釋。我需要在運行時將我的模型添加到該模型的一個實例中。 – Pricey

+0

不,他們沒有。 – Marco

回答

0

我的問題與我如何支持驗證這個複雜模型有關。我已經更加關注Fluent Validation,並且擁有我需要爲複雜模型執行自定義規則的所有事情,即檢查模型中對象集合的值。

2

我在2001年用MVC模式和servlet一起工作,2006年又用MVC框架在ASP.NET之上實現了自定義MVC框架,並且看看現在人們在做什麼讓我相信大多數人都不關心看MVC代表什麼,只是解釋模型的廢話。許多使用ASP.net MVC的開發人員傾向於將來自客戶端的數據綁定到模型,但這是一個糟糕的設計。模型包含應該轉發給模板管理器的數據,在大多數情況下,模板管理器是Razor引擎。

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

所以我的建議是:不要鏈接,你從客戶端進入模型的數據。

  • 從客戶端獲取數據,做一個搜索請求對象是否需要
  • 驗證數據(fluentvalidation)
  • 應用業務規則
  • 創建模型
  • 前進模型引擎的模型

也停止使用這些瘋狂的無用的註釋。

+0

你有什麼例子嗎?我認爲你的意思是使用Fluent驗證一些如何在我的'SurveyRowList'視圖模型上1.在將其應用到視圖之前應用客戶端驗證。 2.在發佈表單時應用業務規則來檢查ModelState?我不明白你的意思,然後創建模型並轉發到模板引擎。我也沒有使用註釋,但如果我曾經使用它們,我只在視圖模型上使用它們,該模型專爲視圖而設計,並且與業務模型無關。 – Pricey

+0

不,我是說你應該在驗證數據後才處理模型。我知道這可以非常方便,您不必從請求對象中獲取自己的值,但這是錯誤的語義。模型將數據從控制器傳送到視圖,您不會開始在已有模型的控制器中處理請求,我知道它允許您這樣做,但避免它。 – Marco

+0

您可以爲每個請求創建驗證器類,例如:SaveCostumerDetailsController - > SaveCostumerDetailsValidator。您提供該驗證程序類,一個包含來自請求數據的poco,然後在其上使用fluentvalidator。如果數據是正確的並且僅在之後,則執行所有數據庫工作並創建模型。 – Marco

相關問題