![](/img/trans.png)
[英]No generic method 'Sum' on type 'System.Linq.Enumerable' is compatible
[英]The model item passed into the dictionary is of type 'System.Linq.Enumerable
我没有任何例外地尝试了这段代码。 但是现在,当我再次以相同的方式再次执行此操作时,我得到了这个异常说,
传递到字典中的模型项的类型为“ System.Linq.Enumerable + WhereListIterator`1 [myapp12.Models.Customer]”,但是此字典需要模型项为“ 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();
如果没有获取列表的第一个元素,您实际上是在传递一个可枚举的列表,而不是单个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.