Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP NET MVC包含双我的视图_C#_Asp.net_Asp.net Mvc_View_Controller - Fatal编程技术网

C# ASP NET MVC包含双我的视图

C# ASP NET MVC包含双我的视图,c#,asp.net,asp.net-mvc,view,controller,C#,Asp.net,Asp.net Mvc,View,Controller,我有个小问题,不知道怎么解决 当我在视图和控制器中写入时: @Html.DisplayFor(modelItem => item.Category.Name) return View(db.Product.Include(p=>p.Category).OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid).ToList()); 发生这种情况: 当我将其更改为: @Html.Disp

我有个小问题,不知道怎么解决

当我在视图和控制器中写入时:

@Html.DisplayFor(modelItem => item.Category.Name)

return View(db.Product.Include(p=>p.Category).OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid).ToList());
发生这种情况:

当我将其更改为:

@Html.DisplayFor(modelItem => item.CategoryID)

return View(db.Product.OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid).ToList());
发生这种情况:

所以一切正常,但它不显示类别名称。我怎样才能修好它

编辑1:

public partial class Product
    {
        public int ID { get; set; }
        public int ProductID { get; set; }
        public string Name { get; set; }
        public int CategoryID { get; set; }
        public decimal Price { get; set; }
        public int Promotion { get; set; }
        public string Image1 { get; set; }
        public string Image2 { get; set; }
        public string Image3 { get; set; }
        public string Image4 { get; set; }
        public string Description { get; set; }
        public int ClientID { get; set; }

        public virtual Category Category { get; set; }
        public virtual Client Client { get; set; }
    }

将GroupBy添加到表达式。更改

View(db.Product.Include(p=>p.Category).OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid).ToList());


在我看来,由于某种原因,你会得到多个相同的项目。您可以通过以下方式获得不同的项目:

var results = db.Product
    .Include(p=>p.Category)
    .OrderByDescending(a => a.ProductID)
    .Where(c => c.ClientID == clientid)
    .Distinct()
    .ToList());
return View(results);

但更好的办法是调查为什么会有多个相同的项目。在这种情况下,删除
Distinct()
子句,并在最后一条语句上设置断点。接下来,检查结果。必须有一个属性使Id为4的第一个项与Id为4的第二个项不同。

在OrderByDescending上获取此错误:“IGrouping”不包含“ProductID”的定义,并且找不到接受“IGrouping”类型的第一个参数的扩展方法“ProductID”(是否缺少using指令或程序集引用?)描述:在执行当前web请求期间发生未经处理的异常。请查看堆栈跟踪,以了解有关错误及其起源于代码的详细信息。异常详细信息:System.InvalidOperationException:传递到字典的模型项的类型为“System.Collections.Generic.List
1”[System.Linq.IGrouping
2[System.Int32,AMBIT\u CMS\u MVC.Areas.Admin.Models.Product]],但此词典需要类型为“System.Collections.Generic.IEnumerable`1[AMBIT\u CMS\u MVC.Areas.Admin.Models.Product]”的模型项。当您尝试wirte“GroupBy(P=>P)”时,是否有“ProductID”通过intellisense?你的视图模型是什么?@model IEnumerableDon不知道。它通常保存在DB中-只有一个项目。你的答案只是混合。混合?你是什么意思?你能将
产品
类添加到问题中吗?也有两个项目,但只有按ID排序的是混合。
var results = db.Product
    .Include(p=>p.Category)
    .OrderByDescending(a => a.ProductID)
    .Where(c => c.ClientID == clientid)
    .Distinct()
    .ToList());
return View(results);