我有一个简单的模型类:
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class CustomersController : Controller
{
// GET: Customers
public ActionResult Index()
{
var customers = GetCustomers();
return View(customers);
}
public IEnumerable<Customer> GetCustomers()
{
return new List<Customer>
{
new Customer
{
Id = 1,
Name = "Peter Parker"
},
new Customer
{
Id = 2,
Name = "Eddie Brock"
},
new Customer
{
Id = 3,
Name = "Mary Jane"
},
};
}
}
}
在我看来,我引入了模型,并尝试循环浏览客户并将他们打印到一个列表中。不知道我做错了什么,因为英特伦斯不会提出任何问题。我不能在li中说like@customer.name。如果我将鼠标悬停在模型上,@foreach块中的customer,它表示没有定义customer。
@model Vidly.Models.Customer
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<ul>
@foreach(var customer in Model.Customers)
{
<li>@customer</li>
}
</ul>