0

我有以下视图模型,用于表示问题调查,但它们被构造成更扁平的网格以适应默认模型活页夹。

// 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列表。

在此图中可以看到按类型和订单号呈现时的排序示例: 在此处输入图像描述

该图像仅显示了一些简单的示例,每页可以有 1-10 行,最多 100 行,但我还添加了一些我想要应用的验证规则的解释。还有更多,但这些只是几个例子。

我的问题是我想支持这种更复杂的验证,但所有规则和错误文本都存储在数据库中,1. 因为用户配置,2. 因为错误文本的现有本地化支持多种语言。

我正在寻找人们可能必须能够支持这一点的任何建议。

我已经看到了Fluent Validation之类的东西,但我还没有深入研究,但到目前为止,我看不到任何不会在模型上使用数据注释的示例..以及适用的 RequiredIf 或 DisabledIf 或 EnabledIf 样式验证规则跨越稍微复杂的对象集合。

4

2 回答 2

2

I worked with MVC patterns in 2001 with servlets, and again in 2006, with a custom MVC framework implemented on top of ASP.NET, and looking at what people are doing nowadays makes me believe that most did not even care about looking at what MVC stands for, only that explain the models nonsense. A lot of developers working with ASP.net MVC, tend to bind the data that is coming from the client to models, but that is such a poor design. Models contain the data that should be forwarded to the template manager which is in most cases the Razor engine.

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

So my advice is: don't link the data that you get from the client into the models.

  • Get the data from the client, do a search on the Request object if it needs to
  • Validate the data (fluentvalidation)
  • apply the business rules
  • create the models
  • 将模型转发到模板引擎

也停止使用那些疯狂无用的注释。

于 2013-03-29T22:40:15.280 回答
0

我的问题与我如何支持验证这个复杂模型有关。从那以后,我更多地研究了 Fluent Validation,它拥有为复杂模型执行自定义规则所需的一切,即检查模型中对象集合的值。

于 2013-04-02T00:28:54.373 回答