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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:24:11+00:00 2026-05-15T17:24:11+00:00

Right to the point: How would I map a Dictionary<String, String> to a ViewModel?

  • 0

Right to the point: How would I map a Dictionary<String, String> to a ViewModel? The ViewModel contains properties of the same name of the Dictionary keys.

To give more information, the program reads a database record and returns all of the values for the record in a Dictionary<String, String>. I only want to display 20-30 of these fields to the user while other processing happens behind the scenes with the other fields. I’ve made a ViewModel and strongly typed the view. I can manually assign the properties from the Dictionary<String, String> to the ViewModel properties, however this seems like the wrong way to fill the ViewModel.

I then tried using reflection to populate the fields, but again this seems like the wrong way to go about this.

foreach (PropertyInfo propertyInfo in this.GetType().GetProperties())
{
    if (propertyInfo.PropertyType.Name.Equals("String"))
    {
        if (myClass.FieldValues.Keys.Contains(propertyInfo.Name))
        {
            propertyInfo.SetValue(this, myClass.FieldValues[propertyInfo.Name], null);
        }
    }
}

So again, is there a way I can populate my ViewModel using a Dictionary<String, String> where the Key values are the same name as the ViewModel property values?

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

    If I were I’d be looking at setting up an ORM layer (Entity, Linq-to-Sql) to return strongly typed objects. This provides you with a much more type safe overall solution.

    If you are stuck with Dictionary<string,string> then you have three options:

    1. Reflection – you’ve already done that, that’s pretty straight forward.

    2. Expressions/Reflection. You can compile an expression that does the conversion into a lambda once and simply call it as a method afterwards. This has the benefit of being faster at runtime than reflection. However, unless you are doing thousands of these operations per second, performance gained is not worth the extra effort.

    3. dynamic/ExpandoObject. This somewhat experimental and would involve a strongly typed wrapper around a dynamic object.

    public class ViewModel {
      ExpandoObject m_Dynamic;
    
      public string FirstName {return m_Dynamic.FirstName ; }
      public string LastName {return m_Dynamic.LastName; }
    
      public ExpandoObject DynamicObject {get{return m_Dynamic;}}
    }
    
    // In the data mapping layer
    ViewModel vm = new ViewModel();
    Dictionary<string,string> data = new Dictionary<string,string>{{"FirstName", "Igor"}, {"LastName", "Zevaka"}};
    foreach(var kv in data) {
      ((IDictionary<string,object>)vm.DynamicObject)[kv.Key] = kv.Value;
    }
    

    Heck, you can even not bother with dynamic and have the property go to the dictionary dicrectly:

    public class ViewModel {
      Dictionary<string,string> m_Dynamic;
    
      public string FirstName {return m_Dynamic["FirstName"]; }
      public string LastName {return m_Dynamic["LastName"]; }
    
      public Dictionary<string,string> DynamicObject {get {return m_Dynamic;}}
    }
    
    // In the data mapping layer
    ViewModel vm = new ViewModel();
    Dictionary<string,string> data = new Dictionary<string,string>{{"FirstName", "Igor"}, {"LastName", "Zevaka"}};
    foreach(var kv in data) {
      vm.DynamicObject[kv.Key] = kv.Value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 471k
  • Answers 471k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can try turning off antialiasing using CGContextSetShouldAntialias(). Also, 0.25… May 16, 2026 at 3:12 am
  • Editorial Team
    Editorial Team added an answer No, you have to call the pre_save.connect as many number… May 16, 2026 at 3:12 am
  • Editorial Team
    Editorial Team added an answer The 'tweet this link' feature is extremly simple and does… May 16, 2026 at 3:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.