传入字典的模型项的类型为'System.Linq.Enumerable
本文关键字:System Linq Enumerable 类型 字典 模型 | 更新日期: 2023-09-27 18:04:48
我试过这段代码一次,没有任何异常。但是现在当我以同样的方式再次执行时我得到了这个异常,
传入字典的模型项的类型为'System.Linq.Enumerable+WhereListIterator ' 1[myapp12.Models. model]。,但是这个字典需要一个类型为"myapp12.Models.Customer"的模型项。
这是我的代码
CustomerController
**public class CustomerController : Controller
{
//
// GET: /Customer/
List<Customer> CustomerCollection = new List<Customer>();
public CustomerController()
{
Customer cus = new Customer();
cus.CustomerId = 1;
cus.Name = "Mags";
cus.Gender = "Male";
cus.City = "jerks";
CustomerCollection.Add(cus);
cus = new Customer();
cus.CustomerId = 2;
cus.Name = "Jacob";
cus.Gender = "Male";
cus.City = "Wagga";
CustomerCollection.Add(cus);
cus = new Customer();
cus.CustomerId = 3;
cus.Name = "Gags";
cus.Gender = "Male";
cus.City = "NewYork";
CustomerCollection.Add(cus);
}
public ActionResult GetCustomerList()
{
return View(CustomerCollection);
}
public ActionResult GetCustomer(int id)
{
var selectedCustomer = CustomerCollection.Where(p => p.CustomerId == id);
return View(selectedCustomer);
}**
GetCustomerList。aspx(视图)
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<myapp12.Models.Customer>>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>GetCustomerList</title>
</head>
<body>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
<table>
<tr>
<th>
<%: Html.DisplayNameFor(model => model.Name) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.Gender) %>
</th>
<th>
<%: Html.DisplayNameFor(model => model.City) %>
</th>
<th></th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.Name) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Gender) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.City) %>
</td>
<td>
<%: Html.ActionLink("Edit", "Edit", new { id=item.CustomerId }) %>
<%: Html.ActionLink("Details", "GetCustomer", new { id = item.CustomerId })%>
<%: Html.ActionLink("Delete", "Delete", new { id=item.CustomerId }) %>
</td>
</tr>
<% } %>
</table>
</body>
</html>
GetCustomer.aspx(view)这是获取特定用户的详细信息。
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<myapp12.Models.Customer>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>GetCustomer</title>
</head>
<body>
<fieldset>
<legend>Customer</legend>
<div class="display-label">
<%: Html.DisplayNameFor(model => model.Name) %>
</div>
<div class="display-field">
<%: Html.DisplayFor(model => model.Name) %>
</div>
<div class="display-label">
<%: Html.DisplayNameFor(model => model.Gender) %>
</div>
<div class="display-field">
<%: Html.DisplayFor(model => model.Gender) %>
</div>
<div class="display-label">
<%: Html.DisplayNameFor(model => model.City) %>
</div>
<div class="display-field">
<%: Html.DisplayFor(model => model.City) %>
</div>
</fieldset>
<p>
<%: Html.ActionLink("Edit", "Edit", new { id=Model.CustomerId }) %> |
<%: Html.ActionLink("Back to List", "GetCustomerList") %>
</p>
</body>
</html>
当我第一次做的时候,这对我第一次有效。但现在不行了?我该怎么办?
你有
var selectedCustomer =
CustomerCollection.Where(p => p.CustomerId == id);
应该放在
的地方 var selectedCustomer =
CustomerCollection.Where(p => p.CustomerId == id).FirstOrDefault();
如果不获取列表的第一个元素,则实际上传递的是一个可枚举列表,而不是单个元素。