Error: asp.net MVC-5 model item to IEnumerable
Error: asp.net MVC-5 model item to IEnumerable
我正在使用 .NET MVC-5。我收到上述错误。
在我的例子中,我有一个名为 "Customer-controller" 的控制器。其中包含两个名为 "Index" 和 "Details". 的 Action Results
Index 正在呈现数据库中的客户列表。问题是如果我单击客户列表中的任何名称(由 "Index" 呈现)应该将我重定向到详细信息操作结果并显示与特定客户相关的详细信息。
客户控制员
public class CustomerController : Controller
{
private ApplicationDbContext _context;
public CustomerController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET: Customer
public ActionResult Index()
{
var customers = _context.Customers.ToList();
return View(customers);
}
public ActionResult Details(int id)
{
var cus = _context.Customers.FirstOrDefault(c=> c.Id==id);
if (cus == null)
return HttpNotFound();
return View(cus);
}
}
索引cshtml
<table class="table table-bordered table-responsive table-hover">
<thead>
<tr>
<th>Customer Name</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model)
{
<tr>
<td>@Html.ActionLink(@i.Name, "Details", "Customer", new {id=1 }, null)</td>
</tr>
}
</tbody>
详细cshtml
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>Customer Id</th>
<th>Customer Name</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model)
{
<tr>
<td> @i.Id @i.Name </td>
</tr>
}
</tbody>
您正在使用 FirstOrDefault
选择数据,因此它将 return Customers
class 的单个实体对象,而您正在尝试迭代它,这是错误的。
这里,可以用2种方式解决。
1) 在这里您将获得不带 foreach
的对象值,如下所示。
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>Customer Id</th>
<th>Customer Name</th>
</tr>
</thead>
<tbody>
<tr>
<td> @i.Id @i.Name </td>
</tr>
</tbody>
2) 如果您不想更改视图代码,那么您需要从控制器传递 list
对象,如下所示。
public ActionResult Details(int id)
{
var cus = _context.Customers.FirstOrDefault(c=> c.Id==id);
List<Customers> lstcus = new List<Customers>();
lstcus.Add(cus);
if (cus == null)
return HttpNotFound();
return View(lstcus);
}
我正在使用 .NET MVC-5。我收到上述错误。 在我的例子中,我有一个名为 "Customer-controller" 的控制器。其中包含两个名为 "Index" 和 "Details". 的 Action Results
Index 正在呈现数据库中的客户列表。问题是如果我单击客户列表中的任何名称(由 "Index" 呈现)应该将我重定向到详细信息操作结果并显示与特定客户相关的详细信息。
客户控制员
public class CustomerController : Controller
{
private ApplicationDbContext _context;
public CustomerController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET: Customer
public ActionResult Index()
{
var customers = _context.Customers.ToList();
return View(customers);
}
public ActionResult Details(int id)
{
var cus = _context.Customers.FirstOrDefault(c=> c.Id==id);
if (cus == null)
return HttpNotFound();
return View(cus);
}
}
索引cshtml
<table class="table table-bordered table-responsive table-hover">
<thead>
<tr>
<th>Customer Name</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model)
{
<tr>
<td>@Html.ActionLink(@i.Name, "Details", "Customer", new {id=1 }, null)</td>
</tr>
}
</tbody>
详细cshtml
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>Customer Id</th>
<th>Customer Name</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model)
{
<tr>
<td> @i.Id @i.Name </td>
</tr>
}
</tbody>
您正在使用 FirstOrDefault
选择数据,因此它将 return Customers
class 的单个实体对象,而您正在尝试迭代它,这是错误的。
这里,可以用2种方式解决。
1) 在这里您将获得不带 foreach
的对象值,如下所示。
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>Customer Id</th>
<th>Customer Name</th>
</tr>
</thead>
<tbody>
<tr>
<td> @i.Id @i.Name </td>
</tr>
</tbody>
2) 如果您不想更改视图代码,那么您需要从控制器传递 list
对象,如下所示。
public ActionResult Details(int id)
{
var cus = _context.Customers.FirstOrDefault(c=> c.Id==id);
List<Customers> lstcus = new List<Customers>();
lstcus.Add(cus);
if (cus == null)
return HttpNotFound();
return View(lstcus);
}