Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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
Asp.net mvc 3 添加到类型实体问题的ICollection_Asp.net Mvc 3_Entities_Icollection - Fatal编程技术网

Asp.net mvc 3 添加到类型实体问题的ICollection

Asp.net mvc 3 添加到类型实体问题的ICollection,asp.net-mvc-3,entities,icollection,Asp.net Mvc 3,Entities,Icollection,有人能帮我确定我错在哪里吗 在我的控制器中: [HttpPost] public ActionResult SaveRecommendedUserDetails(RecommendAFriendViewModel model) { //List<Entities.Group.Group> entityGroups = new List<Entities.Group.Group>(); foreach (var group in model.

有人能帮我确定我错在哪里吗

在我的控制器中:

 [HttpPost]
 public ActionResult SaveRecommendedUserDetails(RecommendAFriendViewModel model)
 {
    //List<Entities.Group.Group> entityGroups = new List<Entities.Group.Group>();

        foreach (var group in model.Groups)
        {
          Entities.Group.Group entityGroup = new Entities.Group.Group();
          entityGroup.GroupId = group.GroupId;
          //entityGroups.Add(entityGroup);
          recommendedUser.Groups.Add(entityGroup); //groups in recommendeduser is already of type ICollection.
        }
 }
[HttpPost]
公共操作结果保存RecommendedUserDetails(RecommendAFriendViewModel模型)
{
//List entityGroups=新列表();
foreach(model.Groups中的var组)
{
Entities.Group.Group entityGroup=新的Entities.Group.Group();
entityGroup.GroupId=group.GroupId;
//entityGroups.Add(entityGroup);
RecommendeUser.Groups.Add(entityGroup);//RecommendeUser中的组已为ICollection类型。
}
}
RecommendAFriendViewModel模型组属性:

 public IEnumerable<DataModels.Group.GroupDataModel> Groups { get; set; }
  public virtual ICollection<Group.Group> Groups { get; set; }
public IEnumerable组{get;set;}
推荐用户实体组属性:

 public IEnumerable<DataModels.Group.GroupDataModel> Groups { get; set; }
  public virtual ICollection<Group.Group> Groups { get; set; }
公共虚拟ICollection组{get;set;}
在我得到的两条红线上:无法从“int”转换为“zing.Entities.Group.Group”

and:system.Collections.Generic.List.Add(zing.Entities.Group.Group)“”的最佳重载方法匹配的参数无效

请让我看看我做错了什么好吗?
谢谢

您从未初始化过
entityGroups
变量。在使用变量之前,应确保已分配该变量。您的
entityGroup
的范围也可以移动到
foreach
循环:

[HttpPost]
public ActionResult SaveRecommendedUserDetails(RecommendAFriendViewModel model)
{
    var entityGroups = new List<Entities.Group.Group>();
    foreach (var group in model.Groups)
    {
        if (!recommendedUser.Groups.Any(x => x.GroupId == group.GroupId))
        {
            var entityGroup = new Entities.Group.Group();
            entityGroup.GroupId = group.GroupId;
            entityGroups.Add(entityGroup.GroupId);
        }
    }
    recommendedUser.Groups.Add(entityGroups);
}
[HttpPost]
公共操作结果保存RecommendedUserDetails(RecommendAFriendViewModel模型)
{
var entityGroups=新列表();
foreach(model.Groups中的var组)
{
如果(!RecommendeUser.Groups.Any(x=>x.GroupId==group.GroupId))
{
var entityGroup=new Entities.Group.Group();
entityGroup.GroupId=group.GroupId;
添加(entityGroup.GroupId);
}
}
推荐用户组。添加(entityGroups);
}

您没有在每次迭代
foreach
循环时实例化新的
Entities.Group.Group
对象。您所做的只是覆盖上一个
entityGroup.GroupId
Property集,然后尝试将相同的实体对象添加到集合中,而该集合已经在上一次迭代中

将entityGroup变量声明移动到
if
语句中应该可以解决这个问题

if (!recommendedUser.Groups.Any(x => x.GroupId == group.GroupId))
{
  Entities.Group.Group entityGroup = new Entities.Group.Group(); // here
  entityGroup.GroupId = group.GroupId;
  entityGroups.Add(entityGroup); //get red line here
}

谢谢,我想了这么多,但我仍然在.add行上看到红线,表明最佳重载方法匹配..有一些无效的参数。噢,嗯,我刚刚注意到您正试图将GroupId添加到组对象集合中。您只需要添加entityGroup对象-我更新了我的示例感谢我将一行移到foreach中,并将ICollection更改为List,但在RecommendeUser.Groups.add(entityGroups)上仍会出现红线;即使在我移动了新实体之后,也会出现与另一条红线相同的“错误”。Group.Group();要进入Foreach,建议用户做什么?我看不到您在任何地方声明此变量。当您尝试使用我的代码时,您收到了什么编译错误消息?与“System.Collections.Generic.ICollection.Add(Zinc.Entities.Group.Group)”匹配的最佳重载方法“具有一些无效参数,无法从System.Collections.Generic.List”转换为“ZINK.Entities.Group.Group”,您在我的回答中看到了
ICollection
?请再仔细阅读我的答案。