How can I loop through a List of type Object?
List<object> countries = new List<object>();
countries.Add(new { Name = "United States", Abbr = "US" , Currency = "$"});
countries.Add(new { Name = "Canada", Abbr = "CA", Currency = "$" });
...more
I want to do something like (using property names) in my view
@model ViewModel
@foreach(object country in Model.Countries)
{
Name = country.Name
Code = country.Abbr
Currency = country.Currency
}
UPDATE:
Forgot to mention that I am using MVC and I want to loop the data in View. Countries object is one of the property of ViewModel to view is strongly typed.
UPDATE:
updating as asked to show how View is called from the controller –
[HttpPost]
public ActionResult Index(FormCollection form)
{
..some validations and some logic
ViewModel myViewModel = new ViewModel();
myViewModel.Countries = GetCountries(); -- this is where data get initialized
myViewModel.Data = db.GetData();
return PartialView("_myPartial", myViewModel);
}
If I understood well, you are trying to send the view model from the controller to the view. So if you are using razor your code should be like this
notice the keyword
Model.Edit
Hope it helps you.