我有一个简单的Customer模型列表,我将其传递给我的视图。我想遍历客户类,但我的观点告诉我通过返回一个空白屏幕来关闭。请告诉我这里有什么问题?
模特课程:
public class Customer
{
public string CustomerName { get; set; }
public int Age { get; set; }
}
家庭控制器:
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);
}
查看HTML文件
@model IEnumerable<ASPMVC_Database.Models.Customer>
/...some unrelated code
@foreach( var item in Model)
{
@item.FullName <br/>
}
上面这个简单的代码没有给我什么。只是一个白色的屏幕。我在这里错过了什么?
谢谢
答案 0 :(得分: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);
}