ASP.NET MVC 2 filling ViewModel data


ASP.NET MVC 2 filling ViewModel data



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?


Using System.ComponentModel.DisplayName with dynamic values?

1:

Strongly typed user control model binding not working in Asp.Net MVC 2.0
If I were I'd be looking at setting up an ORM layer (Entity, Linq-to-Sql) to return strongly typed objects. MVC2 Partial View throws/catches exceptions while resolving the path to the viewThis provides you with a enough more type safe overall solution.. Send fax from ASP.NET MVC application If you are stuck with Dictionary<string,string> then you have three options:. Html.ValidationMessageFor on Mono has strange results
  1. Reflection - you've already done that, that's pretty straight forward.. how to get the string from dropdown list
  2. Expressions/Reflection. ASP.Net MVC Blog content storageYou must compile an expression this does the conversion into a lambda once and simply call it as a method afterwards. Invalidate portions of the asp.net mvc output cache - with varying levels of granularityThis 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 any what 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 {receive {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 must even not banother 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 {receive  {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; } 


94 out of 100 based on 39 user ratings 554 reviews

*