![](/img/trans.png)
[英]How to implement custom property validation without data annotations and model
[英]Custom model & client validation in MVC without data annotations
我有以下视图模型,用于表示问题调查,但它们被构造成更扁平的网格以容纳默认模型绑定器。
// 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样式验证规则适用跨越稍微复杂的对象集合。
我在2001年使用servlet处理MVC模式,并在2006年再次使用在ASP.NET之上实现的自定义MVC框架,并查看人们现在正在做什么让我相信大多数人甚至不关心看什么是MVC代表,只是解释模型废话。 许多使用ASP.net MVC的开发人员倾向于将来自客户端的数据绑定到模型,但这是一个糟糕的设计。 模型包含应转发给模板管理器的数据,模板管理器在大多数情况下是Razor引擎。
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
所以我的建议是:不要将从客户端获得的数据链接到模型中。
也停止使用那些疯狂无用的注释。
我的问题与我如何支持验证这个复杂的模型有关。 我从那时起就更多地关注Fluent Validation,它拥有为复杂模型做自定义规则所需的一切,即检查模型中对象集合的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.