加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 基于DataType的模型验证

发布时间:2020-12-15 21:31:32 所属栏目:百科 来源:网络整理
导读:我有一个项目,具有经典的3层结构:DataStore,BusinessLogic,Web-Frontend 在DataStore中,我有一个模型(简化),例如ConfigModel.cs: public class ConfigModel{ [DataType(DataType.EmailAddress)] public string DefaultSenderEmail { get; set; } public IP
我有一个项目,具有经典的3层结构:DataStore,BusinessLogic,Web-Frontend

在DataStore中,我有一个模型(简化),例如ConfigModel.cs:

public class ConfigModel
{

    [DataType(DataType.EmailAddress)]
    public string DefaultSenderEmail { get; set; }

    public IPAddress FallbackDNS { get; set; }

}

问题出在这里:

根据实际的DataType或DataType属性以编程方式添加Validators的优雅方法是什么?

到目前为止,我已经考虑过几个答案,但没有找到令人满意的答案:

>为参数添加[EmailAddress]验证属性:我不想复制,我不希望在我的DataStore层中引用任何MVC特定代码.
>制作单独的ViewModel并使用AutoMapper:由于我的某些模型比这复杂得多,我讨厌制作特定的ViewModel.

谢谢!

解决方法

我也会考虑使用AutoMapper,但不能作为这个解决方案的答案.

也许你可以考虑这个:http://weblogs.asp.net/srkirkland/archive/2011/02/15/adding-client-validation-to-dataannotations-datatype-attribute.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读