最佳实践:ASP.NET MVC 中的数据库类到对象类进行验证
在使用 NHibernate 的项目中,我有这个类:
public class AdminVAT : IAdminDecimal
{
public virtual int Id { get; set; }
public virtual int Code { get; set; }
public virtual decimal Value { get; set; }
}
在 ASP.NET MVC 中,我想要对某些字段进行一些验证或/和一些格式化。然后我尝试了这个,使用 AutoMapper :
public class AdminVATDTO : AdminVAT
{
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public override decimal Value { get; set; }
}
在控制器中:
IList<AdminVAT> listAdminVAT = new AdministrationService(session).ListDecimal<AdminVAT>();
AutoMapper.Mapper.CreateMap<AdminVAT, AdminVATDTO>();
model.ListVAT = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);
在 HTML 中:
@Html.DropDownList("ddVAT", new SelectList(Model.ListVAT, "Id", "Value", Model.Estimation.AdminVAT))
“值”字段是一个小数,当我在页面上显示它时,我有 5 个小数,我需要 2。这里的请求很简单,但可能是稍后在其他课程的其他领域会更复杂。在这种情况下,这不起作用,但可以使用相同的方式处理其他类。
以前有人(Darin)告诉我,使用 AutoMapper 来做到这一点并不是一个好方法……问题是。这是最好的方法吗?
编辑:cshtml 文件中没有代码
谢谢,
In a project using NHibernate, I have this class :
public class AdminVAT : IAdminDecimal
{
public virtual int Id { get; set; }
public virtual int Code { get; set; }
public virtual decimal Value { get; set; }
}
In ASP.NET MVC, I'd like some validation or/and some formatting on some fields. Then I tried this, using AutoMapper :
public class AdminVATDTO : AdminVAT
{
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public override decimal Value { get; set; }
}
In the Controller :
IList<AdminVAT> listAdminVAT = new AdministrationService(session).ListDecimal<AdminVAT>();
AutoMapper.Mapper.CreateMap<AdminVAT, AdminVATDTO>();
model.ListVAT = AutoMapper.Mapper.Map<IList<AdminVAT>, IList<AdminVATDTO>>(listAdminVAT);
In the HTML :
@Html.DropDownList("ddVAT", new SelectList(Model.ListVAT, "Id", "Value", Model.Estimation.AdminVAT))
The "Value" field is a decimal, when I display it on the page, I have 5 decimal, I need 2. The request here is simple but may be more complexe later on other field in other classes. In this case, that's not work, but work on other classes using the same way.
In a previous past someone (Darin) told me it was not a good way to use AutoMapper to do this ... the question is. Wich is thge best way to do this ?
Edit : and no code in the cshtml file
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕您的模型存在一些严重问题。看起来好像这个 AdminVATDTO 旨在作为视图模型,前提是它具有一些格式化属性,但视图模型永远不应该从模型派生。这不是正确的设计。
这是一个正确的设计:
域模型(不改变它,因为我认为它已经存在):
视图模型:
然后定义
AdminVAT
和AdminVATViewModel
之间的映射:然后在您的控制器中行动:
最后在视图中:
I am afraid that there are some serious issues with your models. It seems as if this
AdminVATDTO
is intended as a view model provided the fact that it has some formatting attributes on it but view models should never derive from models. That's not correct design.Here's a correct design:
Domain model (not changing this as I suppose this already exists):
View models:
then define a mapping between
AdminVAT
andAdminVATViewModel
:and then in your controller action:
and finally in the view: