Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6039621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:23:46+00:00 2026-05-23T06:23:46+00:00

In a project using NHibernate, I have this class : public class AdminVAT :

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:23:47+00:00Added an answer on May 23, 2026 at 6:23 am

    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")
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using NHibernate for a project. This project opens a SQL Server 2005 database
I'm using NHibernate 2 and PostgreSQL in my project. SchemaExport class does a great
in my mvc project im using nhibernate 3 as orm, but i have a
I have a Project and a Person (somewhat simplified): public class Project { public
I have the classes Foo and Bar : public class Foo { public int
I am trying to develop a .NET Web Project using NHibernate and Spring.NET, but
I'm using NHibernate on a project and I need to do data auditing. I
I am using NHibernate on a new ASP.NET project, and am running into what
I have a test project using MbUnit and TestDriven.Net. If I right-click on an
In my project I am using NHibernate/FluentNHibernate, and I am working with two entities,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.