4

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);
}

You need to make countries anonymous too.

As an exaplme, something like

var countries = (new[] {
    new { Name = "United States", Abbr = "US", Currency = "$" },
    new { Name = "Canada", Abbr = "CA", Currency = "$" },
 });
 List<string> names = new List<string>();
 countries.ToList().ForEach(x => { names.Add(x.Name); });
4

6 回答 6

8
var countries = new []{
        new { Name = "United States", Abbr = "US", Currency = "$" },
        new { Name = "Canada", Abbr = "CA", Currency = "$" }
    };

foreach(var country in countries)
{
      var Name = country.Name;
      .....
}
于 2012-09-26T16:22:50.377 回答
3

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

@model ViewModel
@foreach(object country in Model.countries)
{
  var Name = country.Name
  var Code = country.Abbr
  var Currency = country.Currency
}

notice the keyword Model.

Edit

// Code inside your controller should be like this
ViewModel myModel = new ViewModel();
List<object> countries = new List<object>();
countries.Add(new { Name = "United States", Abbr = "US" , Currency = "$"});
countries.Add(new { Name = "Canada", Abbr = "CA", Currency = "$" });

myModel.countries = countries;

return View("yourView", myModel); // you can write just return View(myModel); if your view's name is the same as your action 

Hope it helps you.

于 2012-09-26T16:54:45.710 回答
3

您还需要使国家/地区匿名。

例如,类似

var countries = (new[] {
    new { Name = "United States", Abbr = "US", Currency = "$" },
    new { Name = "Canada", Abbr = "CA", Currency = "$" },
 });
 List<string> names = new List<string>();
 countries.ToList().ForEach(x => { names.Add(x.Name); });
于 2012-09-26T16:19:58.713 回答
1

I would just create a new class, and use that instead of the generic object. Is there a reason that it needs to use the base level object? If more abstraction is needed you could utilize an anonymous type with a Where clause or use an abstract class.

于 2012-09-26T16:20:55.710 回答
1

The way you defined your objects leaves dynamic as your only option: the two anonymous classes are of different type. You should either write

foreach (dynamic country in countries) {
    ...
}

or initialize your list with instances of a named class (this is preferred, because dynamic may be too heavy in your situation).

于 2012-09-26T16:21:41.483 回答
0

If you want to work with the polymorphic items somehow (and not with anonymous types) take a look at Cast<...>.ToList() or OfType<...>.ToList()

于 2012-09-26T16:21:56.150 回答