我是ASP.NET新手,我使用Udemy的视频学习ASP.NET 这是我的代码
Customer.cs(Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Vidly.Models
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
}
CustomersController.cs(Controller)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;
namespace Vidly.Controllers
{
public class CustomersController : Controller
{
//
// GET: /Customers/
public ActionResult Index()
{
var customers = new List<Customer>
{
new Customer {Name = "Mahdi" },
new Customer {Name = "Yaser" },
new Customer {Name = "Zahra" },
new Customer {Name = "Amir" },
};
return View(customers);
}
}
}
Index.cshtml(查看)
@model Vidly.Models.Customer
<h3>Customer page</h3>
<table class="table table-striped table-hover table-‐bordered">
<thead>
<tr>
<th>Customer</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td><a href="#"> @customer.Name </a></td>
</tr>
}
</tbody>
</table>
视觉工作室告诉我问题是我@foreach
,但我不知道如何解决它!
谢谢你的帮助;)
答案 0 :(得分:2)
更改
@model Vidly.Models.Customer
要
@model IEnumerable<Vidly.Models.Customer>
答案 1 :(得分:2)
在index.cshtml
中,检查您的型号。
您必须将其替换为:
@model List<Vidly.Models.Customer>