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 5948267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:05:00+00:00 2026-05-22T17:05:00+00:00

This is probably quite straight forward for some, however I’m a bit confused and

  • 0

This is probably quite straight forward for some, however I’m a bit confused and can’t find a decent example. Say I’m using view models and my POST action takes in that view model. Typically I would do something along the following lines:

    [HttpPost]
    public ActionResult Update(UserViewModel uvm)
    {
        User user = Mapper.Map<UserViewModel, User>(uvm);
        _repository.Update(user);

        return RedirectToAction("Index");
    }

Although this isn’t the full picture. The mapping would work fine, however if I were to just update what I’ve mapped then it’d get rid of valuable data in the database because of course in this case I’m not updating the password or other details.

My repository looks something like this:

    public void Update(User user)
    {
        User u = Session.QueryOver<User>().Where(x => x.UserName == user.UserName).SingleOrDefault();

        if (u == null)
            throw new Exception("User not found");

        u.Forename = user.Forename;
        u.Surname = user.Surname;
        u.EmailAddress = user.EmailAddress;
    }

[I’m using NHibernate so it’ll save the object back to the DB once the session is closed (after the request has finished) automatically for me.]

So my question is, in my repository should I load the “User” entity, then update the values I want, and then save it back, or is there another method to do this? The reason I ask is because it seems a bit… “manual” if you see what I mean? Perhaps it is correct, but I just wanted to see opinions of those with more experience in this area.

Cheers

  • 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-22T17:05:00+00:00Added an answer on May 22, 2026 at 5:05 pm

    I use the following approach:

    [HttpPost]
    public ActionResult Update(UserViewModel uvm)
    {
        User user = _userRepository.FindById(uvm.Id);
    
        user.Forename = uvm.Forename;
        user.Surname = uvm.Surname;
        user.EmailAddress = uvm.EmailAddress;
    
        _userRepository.Update(user);
    
        return RedirectToAction("Index");
    }
    

    UPDATE:

    To address the comments about AutoMapper here’s how to proceed:

    Let’s take for example the following classes:

    public class UserViewModel
    {
        public string Forename { get; set; }
        public string Surname { get; set; }
        public string EmailAddress { get; set; }
    }
    
    public class User
    {
        public string Forename { get; set; }
        public string Surname { get; set; }
        public string EmailAddress { get; set; }
    
        public string Password { get; set; }
    }
    

    We don’t want to modify the user password in the UI. So we express our intention to AutoMapper:

    Mapper
        .CreateMap<UserViewModel, User>()
        .ForMember(dest => dest.Password, opt => opt.Ignore());
    

    and then:

    [HttpPost]
    public ActionResult Update(UserViewModel uvm)
    {
        // Fetch the original model we would like to update
        User user = _userRepository.FindById(uvm.Id);
    
        Mapper.Map(uvm, user);
        // At this stage the user model will have its 
        // Forename, Surname and EmailAddress properties 
        // updated from the view model and its Password property
        // will remain the one we got from the repository
    
        _userRepository.Update(user);
    
        return RedirectToAction("Index");
    }
    

    UPDATE 2:

    To address the question in the comments about configuring AutoMapper I usually use Profiles:

    public class UsersProfile : Profile
    {
        protected override void Configure()
        {
            Mapper
                .CreateMap<UserViewModel, User>()
                .ForMember(dest => dest.Password, opt => opt.Ignore());    
    
            Mapper
                .CreateMap<User, UserViewModel>();
        }
    }
    

    and then have a registry class which registers all the mappers:

    public class MappingsRegistry
    {
        public static void Configure()
        {
            Mapper.AddProfile(new UsersProfile());
            Mapper.AddProfile(new SomeOtherProfile());
            ...
        }
    }
    

    which is called in Application_Start:

    MappingsRegistry.Configure();
    

    Finally my controllers have a reference to the mapping engine:

    public class UsersController : Controller
    {
        private readonly IUsersRepository _repository;
        private readonly IMappingEngine _mappingEngine;
        public ContratsFCController(IUsersRepository repository, IMappingEngine mapperEngine)
        { 
            _repository = repository;
            _mapperEngine = mapperEngine;
        }
    
        [AutoMap(typeof(User), typeof(UserViewModel))]
        public ActionResult Update(int id)
        {
            var user = _repository.FindById(id);
            return View(user);
        }
    
        [HttpPost]
        public ActionResult Update(UserViewModel uvm)
        {
            if (!ModelState.IsValid)
            {
                return View(uvm);
            }
            var user = _repository.FindById(uvm.Id);
            _mapperEngine.Map(uvm, user);
            _repository.Update(user);
            return RedirectToAction("Index");
        }
    }
    

    Now all that’s left is to instruct your DI framework to pass the Mapper.Engine property to the constructor and in your unit tests obviously substitute them with an appropriate mock.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably quite straight-forward but at the moment I can't think of a
This is probably quite simple but I've googled and can't find an answer. I
This is probably something quite easy to resolve, however, I am having some difficulties
This is probably something quite easy to resolve, but for some reason, I can't
this is probably quite a simple problem, however, I can't figure out how to
I know this is probably quite straightforward but cannot seem to find an example
This is probably something really simple, however I am quite new to PHP, and
This is probably quite simple to do.. but I just can't think of how
This is probably quite a simple question, but I can't remember how to do
This is probably a very obvious question but I can't quite figure it out.

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.