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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:16:09+00:00 2026-06-09T17:16:09+00:00

So lets say I have an object class MyItem { public string Name {get;

  • 0

So lets say I have an object

class MyItem
{
     public string Name {get; set;}
     public string Description {get; set;}
}

I want to create a ViewModel

class MyItemViewModel
{
     public string Name {get; set;}
     public string Description {get; set;}
     public string Username {get; set;}
}

I would like to on the controller get an object of type MyItem and automatically populate the ViewModel. With the values contained within MyItem (ie if it has a property called Name, automatically fill it out).

What I am trying to avoid is a list of Model.Name = Item.Name rows. MyItemViewModel will also have different attributes and display values so I cannot simply embed MyItem within the View Model.

Is there a clean programmatical way of duplicating attributes of the same name and type between objects or am I stuck by doing it by hand?

  • 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-09T17:16:11+00:00Added an answer on June 9, 2026 at 5:16 pm

    You could use AutoMapper for this task. I am using it in all projects to map between my domain models and view models.

    You simply define a mapping in your Application_Start:

    Mapper.CreateMap<MyItem, MyItemViewModel>();
    

    and then perform the mapping:

    public ActionResult Index()
    {
        MyItem item = ... fetch your domain model from a repository
        MyItemViewModel vm = Mapper.Map<MyItem, MyItemViewModel>(item);
        return View(vm);
    }
    

    and you could write a custom action filter which overrides the OnActionExecuted method and substitues the model that was passed to the view with the corresponding view model:

    [AutoMap(typeof(MyItem), typeof(MyItemViewModel))]
    public ActionResult Index()
    {
        MyItem item = ... fetch your domain model from a repository
        return View(item);
    }
    

    This makes your controller actions pretty simple.

    AutoMapper has another very useful method which could be used in your POST actions when you want to update something:

    [HttpPost]
    public ActionResult Edit(MyItemViewModel vm)
    {
        // Get the domain model that we want to update
        MyItem item = Repository.GetItem(vm.Id);
    
        // Merge the properties of the domain model from the view model =>
        // update only those that were present in the view model
        Mapper.Map<MyItemViewModel, MyItem>(vm, item);
    
        // At this stage the item instance contains update properties
        // for those that were present in the view model and all other
        // stay untouched. Now we could persist the changes
        Repository.Update(item);
    
        return RedirectToAction("Success");
    }
    

    Imagine for example that you had a User domain model containing properties like Username, Password and IsAdmin and that you have a form allowing the user for changing his username and password but absolutely not changing the IsAdmin property. So you would have a view model containing the Username and Password properties bound to an html form in the view and using this technique you will only update those 2 properties, leaving the IsAdmin property untouched.

    AutoMapper also works with collections. Once you have defined a mapping between the simple types:

    Mapper.CreateMap<MyItem, MyItemViewModel>();
    

    you don’t need to do anything special when mapping between collections:

    IEnumerable<MyItem> items = ...
    IEnumerable<MyItemViewModel> vms = Mapper.Map<IEnumerable<MyItem>, IEnumerable<MyItemViewModel>>(items);
    

    So wait no more, type the following command in your NuGet console and enjoy the show:

    Install-Package AutoMapper
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an object class Person { public string Name { get;
Lets say I have object Tom which has class Person. Class Person String Name
Lets say I have class Person { public Person(int age, string name) { Age
Lets say I have business class: public class Foo { public int Prop1 {get;set;}
Let's say I have an object: public class CustomObj { DateTime Date { get;
Lets say I have an abstract class: abstract class MyAbstract { protected abstract object
Let's say we have a collection of Person objects class Person { public string
Lets say I have a class like this in class1.vb: Public Class my_class Public
Lets say I have the following code: public class Base { // Some stuff
Lets say I have a class like this in Java: public class Function {

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.