Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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内核返回_C#_Asp.net_Entity Framework_Linq_Asp.net Core - Fatal编程技术网

C# ASP.NET内核返回

C# ASP.NET内核返回,c#,asp.net,entity-framework,linq,asp.net-core,C#,Asp.net,Entity Framework,Linq,Asp.net Core,我正在使用Microsoft Northwind示例数据库。我搭建了数据库,除了下面的代码之外,其他一切都正常工作 我想让这一逻辑发挥作用: select * from Products join Suppliers on Products.customer_id = Suppliers.customer_id where Products.customer_id = 1 我尝试在LINQ中进行此SQL查询的方式(VS2019中没有错误): 公共异步任务索引() { var测试=等待(来

我正在使用Microsoft Northwind示例数据库。我搭建了数据库,除了下面的代码之外,其他一切都正常工作

我想让这一逻辑发挥作用:

select * from Products
join Suppliers
   on Products.customer_id = Suppliers.customer_id
where Products.customer_id = 1
我尝试在LINQ中进行此SQL查询的方式(VS2019中没有错误):

公共异步任务索引()
{
var测试=等待(来自ep in_context.Products
在ep上加入e。供应商ID等于e。供应商ID
其中(e.供应商ID==1)
选择新的
{ep.ProductName,e.CompanyName}).toListSync();
}
返回视图(测试);
启动项目并转到索引后出错:

InvalidOperationException:传入ViewDataDictionary的模型项的类型为“System.Collections.Generic.List
1[f_uAnonymousType1
1[System.String]]”,但此ViewDataDictionary实例需要类型为“System.Collections.Generic.IEnumerable`1[DbFirst.Models.Northwind.Products]”的模型项。

以防万一,我是说这段代码运行良好,但它不是我所需要的:

public async Task<IActionResult> Index()
{
    var test = await _context.Products.Where(x => x.SupplierId == 1).ToListAsync();

    return View(test);
}
公共异步任务索引()
{
var test=wait_context.Products.Where(x=>x.SupplierId==1.toListSync();
返回视图(测试);
}

David是正确的。当您的操作返回匿名类型列表时,您的视图将收到
IEnumerable

可以为视图创建特定的ViewModel:

public class ProductViewModel
{
    public string ProductName{ get; set; }
    public string CompanyName{ get; set; }
}
然后,修改操作代码以返回
列表


阅读错误消息。它表示视图的
@model
指令指定了一种类型(
IEnumerable
),但您正在将匿名对象列表传递给
view()
方法。旁注:使用序列(或列表、集合等)作为视图的模型类型是一种不好的做法。教程使用这种模式来避免创建类(我假设),但它不会放大。在实践中,您通常不得不将不止一个对象传递给视图,并且无论如何都必须为其创建一个容器。相反,要保持一致,从一开始就为每个视图(例如
IndexViewModel
)创建一个模型类,并将它们组织到子文件夹中(例如
Models\HomeModels
for
HomeController
模型)与视图的组织方式类似。创建一个简单的类,该类具有两个字符串属性,分别称为
ProductName
CompanyName
,并将其命名为
MyModel
——将视图的模型设置为
MyModel
——然后将linq语句中的
new
子句更改为
选择新的MyModel{ep.ProductName,e.CompanyName}
。结果将是您不再使用匿名类,并且该模型只有您想要的列值。非常感谢!我在ep.ProductName和e.CompanyName之前添加了ProductName和CompanyName,然后就成功了!我还有一个问题,您如何将多个模型添加到一个视图?或者我需要在Mod中添加新内容艾尔维想更专业些吗?
public class ProductViewModel
{
    public string ProductName{ get; set; }
    public string CompanyName{ get; set; }
}
public async Task<IActionResult> Index()
{
     var test = await (from ep in _context.Products
             join e in _context.Suppliers on ep.SupplierId equals e.SupplierId
             where (e.SupplierId == 1)

             select new ProductViewModel
             {  
                ep.ProductName,
                e.CompanyName
             }).ToListAsync();
}
return View(test);
@model IEnumerable<ProductViewModel>