2

I have a simple List of Customer model class which I am passing to my view. I want to iterate through the customer class but my view is telling me to bugger off by returning a blank screen. Please tell me what is wrong here?

Model Class:

 public class Customer
    {
        public string CustomerName { get; set; }
        public int Age { get; set; }
    }

Home Controller:

public ActionResult Index()
        {
            List<Customer> customers = new List<Customer>();
            Customer customer = new Customer() { FullName = "MrA" ,Age=25};
            Customer customer2 = new Customer() { FullName = "MrB", Age = 125 };
            return View(customers);
        }

View HTML file

@model IEnumerable<ASPMVC_Database.Models.Customer>
/...some unrelated code

  @foreach( var item in Model)
        {
            @item.FullName <br/>               

        }

This simple code above gives me nothing. Just a white screen. What am I missing out here?

Thank you

4

1 回答 1

2

您永远不会将新客户添加到customers,您只需将它们实例化。请尝试以下操作:

public ActionResult Index()
{
    List<Customer> customers = new List<Customer>();
    customers.Add(new Customer { FullName = "MrA", Age = 25});
    customers.Add(new Customer { FullName = "MrB", Age = 125 });
    return View(customers);
}
于 2013-07-29T18:55:42.967 回答