1

I have a ViewModel with a number of different properties (ie string, int, etc) that I need to iterate through in the controller. What is the best way to do this? Here is the ViewModel's defintion:

public class BankListViewModel
{
    public int ID { get; set; }
    public string BankName { get; set; }
    public string EPURL { get; set; }
    public string AssociatedTPMBD { get; set; }
    public string Tier { get; set; }
    public List<BankListAgentId> BankListAgentId { get; set; }
    public List<BankListStateCode> BankListStateCode { get; set; }
}

I need to omit the two lists, however. Any ideas?

EDIT

The purpose of this process is to pass specific items of the view model into three separate objects. The view model was created to combine properties of three separate SQL tables/Models. I am now trying to divide them up appropriately and add the information to the relevant tables. Right now I'm simply going one by one like so:

        BankListMaster banklistmaster = new BankListMaster();
        banklistmaster.AssociatedTPMBD = viewmodel.AssociatedTPMBD;
        banklistmaster.BankName = viewmodel.BankName;
NealR
  • 10,189
  • 61
  • 159
  • 299

1 Answers1

4

Although it's not clear why you would need to iterate over the properties instead of just reading their vaules, you could accomplish this using reflection

var model = new BankListViewModel();
PropertyInfo[] properties = model.GetType().GetProperties();
foreach (var property in properties)
{
    if (property.GetType() != typeof(List<BankListAgentId>))
            {
                //do your thing here
            }

}
Forty-Two
  • 7,535
  • 2
  • 37
  • 54