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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:20:12+00:00 2026-06-02T10:20:12+00:00

I am currently using ViewModels to separate my Views from the actual Model structure.

  • 0

I am currently using ViewModels to separate my Views from the actual Model structure.

For example I have a User persistence entity and a MyProfile ViewModel containing all the information, that a user can change on his own.
For the conversion from User to MyProfile I am using Automapper.

Now after a user posts back his (changed) information, I need to save these. But the information in the ViewModel is not complete, and when AutoMapper creates a User persistence entity from the ViewModel, important information gets lost.

I do not want to expose this information to the View Layer, especially not with hidden form elements.

So i need a way to merge a ViewModel into a persistence entity. Can I do that with AutoMapper, or do I have to do it manually?

Example:

My User class contains ID, Firstname, Lastname, Username and Password. The user should only edit his First and Lastname in his profile. Therefore my ProfileViewModel contains ID, Firstname and Lastname. After posting back the information from the form, Automapper creates a User object from the transferred ProfileViewModel, and in this object only ID, Firstname and Lastname are set. When feeding this Entity to my Repository, I have lost username and password information.

  • 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-06-02T10:20:13+00:00Added an answer on June 2, 2026 at 10:20 am

    So i need a way to merge a ViewModel into a persistence entity. Can I
    do that with AutoMapper, or do I have to do it manually?

    Yes, you can do that with AutoMapper. For example:

    public class Model
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class ViewModel
    {
        public string Name { get; set; }
    }
    
    class Program
    {
        static void Main()
        {
            // define a map (ideally once per appdomain => usually goes in Application_Start)
            Mapper.CreateMap<ViewModel, Model>();
    
            // fetch an entity from a db or something
            var model = new Model
            {
                Id = 5,
                Name = "foo"
            };
    
            // we get that from the view. It contains only a subset of the
            // entity properties
            var viewModel = new ViewModel
            {
                Name = "bar"
            };
    
            // Now we merge the view model properties into the model
            Mapper.Map(viewModel, model);
    
            // at this stage the model.Id stays unchanged because
            // there's no Id property in the view model
            Console.WriteLine(model.Id);
    
            // and the name has been overwritten
            Console.WriteLine(model.Name);
        }
    }
    

    prints:

    5
    bar
    

    And to translate this into a typical ASP.NET MVC pattern:

    [HttpPost]
    public ActionResult Update(MyViewModel viewModel)
    {
        if (!ModelState.IsValid)
        {
            // validation failed => redisplay view 
            return View(viewModel);
        }
    
        // fetch the domain entity that we want to udpate
        DomainModel model = _repository.Get(viewModel.Id);
    
        // now merge the properties
        Mapper.Map(viewModel, model);
    
        // update the domain model
        _repository.Update(mdoel);
    
        return RedirectToAction("Success");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently using System.Web.UI.WebControls.FileUpload wrapped in our own control. We have licenses for Telerik. I
Currently using Xcode 4.2 and I have two view controllers (1 and 2). I
I am currently using Windows Server 2008 Standard and have several Hyper V machines.
I currently have a fairly straight forward app, my first attempt at MVVM using
I'm using Prism's EventAggregator for loosely coupled communication between my module's ViewModels. I have
In asp.net mvc3, I currently have a single file ViewModels.cs which holds all of
I am currently learning MVVM abd the tutorial I have been using, simply uses
I have created a Base-Controller from which all the controllers inherit. Currently this controller
Good evening everyone. I am currently using MVC 3 and I have a viewmodel
currently im using an observable collection called locationItems to to store my objects from

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.