Asp.net mvc 字典需要IEnumerable类型的模型项
我是Linq的MVC新手,在我的项目中,我遇到了一个错误: 传递到字典中的模型项的类型为“System.Boolean”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Parts.PartsLocation]”的模型项 我将MVC与EF一起使用,那么如何返回IEnumerable 这是我的控制器:Asp.net mvc 字典需要IEnumerable类型的模型项,asp.net-mvc,entity-framework,linq,Asp.net Mvc,Entity Framework,Linq,我是Linq的MVC新手,在我的项目中,我遇到了一个错误: 传递到字典中的模型项的类型为“System.Boolean”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Parts.PartsLocation]”的模型项 我将MVC与EF一起使用,那么如何返回IEnumerable 这是我的控制器: public async Task<ActionResult> Index(String aContainer) {
public async Task<ActionResult> Index(String aContainer)
{
var container = from x in db.PartsLocations select x;
var empty = from y in db.PartsLocations select y;
if (!String.IsNullOrEmpty(aContainer))
{
var parent = from a in db.PartsLocations
where (a.LocationName == aContainer)
select a.ParentLocation;
return View(await parent.ToListAsync());
}
empty = empty.Where(r => r.LocationName.Contains(null));
return View(await empty.ToListAsync());
}
公共异步任务索引(字符串容器)
{
var container=从数据库中的x开始。部件位置选择x;
var empty=从数据库中的y开始。部件位置选择y;
如果(!String.IsNullOrEmpty(aContainer))
{
var parent=来自db.PartsLocations中的a
其中(a.LocationName==a容器)
选择a.ParentLocation;
返回视图(等待parent.ToListAsync());
}
empty=empty.Where(r=>r.LocationName.Contains(null));
返回视图(等待空的.toListSync());
}
视图:
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
型号:
namespace Parts
{
using System;
using System.Collections.Generic;
public partial class PartsLocation
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PartsLocation()
{
this.ManufacturingParts = new HashSet<ManufacturingPart>();
}
public int LocationID { get; set; }
public int TypeID { get; set; }
public string LocationName { get; set; }
public string Description { get; set; }
public int PlantID { get; set; }
public Nullable<int> BayID { get; set; }
public Nullable<int> SecurityGroupID { get; set; }
public Nullable<int> ParentLocation { get; set; }
public Nullable<System.DateTime> LastMoved { get; set; }
public string Notes { get; set; }
public virtual LocationType LocationType { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ManufacturingPart> ManufacturingParts { get; set; }
public virtual ManufacturingPlant ManufacturingPlant { get; set; }
}
}
名称空间部分
{
使用制度;
使用System.Collections.Generic;
公共部分类PartsLocation
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共部门职位()
{
this.ManufacturingParts=new HashSet();
}
public int LocationID{get;set;}
公共int类型ID{get;set;}
公共字符串位置名称{get;set;}
公共字符串说明{get;set;}
公共int PlantID{get;set;}
公共可空BayID{get;set;}
公共可为空的SecurityGroupID{get;set;}
公共可为空的ParentLocation{get;set;}
公共可为空的LastMoved{get;set;}
公共字符串注释{get;set;}
公共虚拟位置类型位置类型{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection ManufacturingParts{get;set;}
公共虚拟制造工厂制造工厂{get;set;}
}
}
a容器不为空,则返回选择
a.ParentLocation
else PartsLocation List
视图模型返回值不同,因此请验证返回值是否可以发布模型代码?抱歉,我现在使用模型进行编辑。在传入字符串为null/空的情况下是否会发生这种情况?这应该是满足IEnumerable的返回列表。最突出的是empty=empty.Where(r=>r.LocationName.Contains(null));这看起来应该是空的=empty.Where(r=>null==r.LocationName);当页面加载时,它返回一个空视图。然后传入一个字符串,我希望它返回记录集的整数类型(ParentLocation),其中传入的字符串与列LocationName匹配。好像是Linq in where子句的问题。@Steve Py-当我传入一个有效的字符串类型时,就会发生这种情况。如果为空,将返回一个空视图。嗨,我不知道“两个返回是不同的,所以请验证返回值”是什么意思。你能解释一下吗?
a.ParentLocation
else PartsLocation List