最佳实践:ASP.NET MVC 中的数据库类到对象类进行验证

发布于 2024-11-16 04:53:54 字数 1181 浏览 1 评论 0原文

在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小瓶盖 2024-11-23 04:53:54

恐怕您的模型存在一些严重问题。看起来好像这个 AdminVATDTO 旨在作为视图模型,前提是它具有一些格式化属性,但视图模型永远不应该从模型派生。这不是正确的设计。

这是一个正确的设计:

域模型(不改变它,因为我认为它已经存在):

public class AdminVAT : IAdminDecimal
{
    public virtual int Id { get; set; }
    public virtual int Code { get; set; }
    public virtual decimal Value { get; set; }
}

视图模型:

public class AdminVATViewModel
{
    public int Id { get; set; }
    public string Value { get; set; }
}

public class MyViewModel
{
    public string SelectedVAT { get; set; }
    public IEnumerable<AdminVATViewModel> ListVAT { get; set; }
}

然后定义 AdminVATAdminVATViewModel 之间的映射:

Mapper
    .CreateMap<AdminVAT, AdminVATViewModel>()
    .ForMember(
        dest => dest.Value,
        opt => opt.MapFrom(src => src.Value.ToString("n2"))
    );

然后在您的控制器中行动:

public ActionResult Foo()
{
    IEnumerable<AdminVAT> listAdminVAT = ... fetch from repo
    var model = new MyViewModel
    {
        ListVAT = Mapper.Map<IEnumerable<AdminVAT>, IEnumerable<AdminVATViewModel>>(listAdminVAT)
    };
    return View(model);
}

最后在视图中:

@model MyViewModel

@Html.DropDownListFor(
    x => x.SelectedVAT, 
    new SelectList(Model.ListVAT, "Id", "Value")
)

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):

public class AdminVAT : IAdminDecimal
{
    public virtual int Id { get; set; }
    public virtual int Code { get; set; }
    public virtual decimal Value { get; set; }
}

View models:

public class AdminVATViewModel
{
    public int Id { get; set; }
    public string Value { get; set; }
}

public class MyViewModel
{
    public string SelectedVAT { get; set; }
    public IEnumerable<AdminVATViewModel> ListVAT { get; set; }
}

then define a mapping between AdminVAT and AdminVATViewModel:

Mapper
    .CreateMap<AdminVAT, AdminVATViewModel>()
    .ForMember(
        dest => dest.Value,
        opt => opt.MapFrom(src => src.Value.ToString("n2"))
    );

and then in your controller action:

public ActionResult Foo()
{
    IEnumerable<AdminVAT> listAdminVAT = ... fetch from repo
    var model = new MyViewModel
    {
        ListVAT = Mapper.Map<IEnumerable<AdminVAT>, IEnumerable<AdminVATViewModel>>(listAdminVAT)
    };
    return View(model);
}

and finally in the view:

@model MyViewModel

@Html.DropDownListFor(
    x => x.SelectedVAT, 
    new SelectList(Model.ListVAT, "Id", "Value")
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文