不包含定义"新增] [英] Does not contain a definition for "Add"

查看:163
本文介绍了不包含定义"新增]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使像<一个同样的事情href=\"http://stackoverflow.com/questions/16383494/mvc-4-many-to-many-relation-and-checkboxes\">this线程,但我得到的错误:


  

System.Collections.Generic.IEnumerable不包含添加的定义,并没有扩展方法添加接受型System.Collections.Generic.IEnumerable'的第一个参数可以找到(是否缺少使用指令或程序集引用?)


下面是我的code:

  [HttpPost]
公众的ActionResult创建(动漫动画)
{
    变种DB =新MainDatabaseEntities();
    VAR newanime =新ANIME
    {
        ID_AN = anime.ID_AN,
        TITLE_OR = anime.TITLE_OR,
        TITLE_EN = anime.TITLE_EN,
        GENRES =新的List&LT;流派与GT;()
    };    的foreach(在anime.GENRES.Where VAR selectedAnime(C =&GT; c.isSelected))
    {
        VAR流派=新流派{ID_GE = selectedAnime.ID_GE};
        db.GENRES.Attach(流派);
        newanime.GENRES.Add(流派); &LT; ---这是错误行
    }    db.ANIME.Add(newanime);
    db.SaveChanges();
    返回RedirectToAction(「指数」);
}

ANIME:

 公共部分类ANIME
{
    公众诠释ID_AN {搞定;组; }
    公共字符串TITLE_OR {搞定;组; }
    公共字符串TITLE_EN {搞定;组; }    公共虚拟IEnumerable的&LT;流派与GT; GENRES {搞定;组; }
}

GENRES:

 公共部分类GENRES
{
    公众诠释ID_GE {搞定;组; }
    公共字符串GENRE {搞定;组; }
    公共布尔isSelected {搞定;组; }
    公共虚拟的ICollection&LT;动画及GT; ANIME {搞定;组; }
}

该错误是在 HttpPost newanime.GENRES.Add(流派)。我加了使用System.Linq的所有模型和控制器,但它并不能帮助。任何想法如何解决这个问题?

编辑:

修复此之后一个新的错误来了。我认为这是不相关的上述之一,但我不想垃圾邮件不必要的线程。

错误消息:


  

实体或复杂类型'MainDatabaseModel.GENRES'不能在LINQ到实体查询构造。


相关code:

 公众的ActionResult的Create()
{
    变种DB =新MainDatabaseEntities();
    VAR视图模型=新ANIME
    {
        GENRES = db.GENRES.Select(C =&gt;新建GENRES
        {
            ID_GE = c.ID_GE,
            GENRE = c.GENRE,
            isSelected = FALSE
        })。了ToList()
    };
    返回视图(视图模型);
}


解决方案

试试这个:

 公共部分类ANIME
{    公众诠释ID_AN {搞定;组; }
    公共字符串TITLE_OR {搞定;组; }
    公共字符串TITLE_EN {搞定;组; }    公共虚拟的ICollection&LT;流派与GT; GENRES {搞定;组; } //使用的ICollection这里
}

I'm trying to make the same thing like in this thread, but I'm getting error:

'System.Collections.Generic.IEnumerable' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

Here is my code:

[HttpPost]
public ActionResult Create(ANIME anime)
{
    var db = new MainDatabaseEntities();
    var newanime = new ANIME
    {
        ID_AN = anime.ID_AN,
        TITLE_OR = anime.TITLE_OR,
        TITLE_EN = anime.TITLE_EN,
        GENRES = new List<GENRES>()
    };

    foreach (var selectedAnime in anime.GENRES.Where(c => c.isSelected))
    {
        var genre = new GENRES { ID_GE = selectedAnime.ID_GE };
        db.GENRES.Attach(genre);
        newanime.GENRES.Add(genre); <--- this is the error line
    }

    db.ANIME.Add(newanime);
    db.SaveChanges();
    return RedirectToAction("Index");
}

ANIME:

public partial class ANIME
{
    public int ID_AN { get; set; }
    public string TITLE_OR { get; set; }
    public string TITLE_EN { get; set; }

    public virtual IEnumerable<GENRES> GENRES { get; set; }
}

GENRES:

public partial class GENRES
{
    public int ID_GE { get; set; }
    public string GENRE { get; set; }
    public bool isSelected { get; set; }
    public virtual ICollection<ANIME> ANIME { get; set; }
}

The error is in the line newanime.GENRES.Add(genre) in HttpPost. I added using System.Linq to all models and controllers but it doesn't help. Any ideas how to resolve this?

EDIT:

After repairing this a new error arrived. I think it's not related to above one but I don't want to spam unnecessary threads.

Error message:

The entity or complex type 'MainDatabaseModel.GENRES' cannot be constructed in a LINQ to Entities query.

Related code:

public ActionResult Create()
{
    var db = new MainDatabaseEntities();
    var viewModel = new ANIME
    {
        GENRES = db.GENRES.Select(c => new GENRES
        {
            ID_GE = c.ID_GE,
            GENRE = c.GENRE,
            isSelected = false
        }).ToList()
    };
    return View(viewModel);       
}

解决方案

Try this:

public partial class ANIME
{

    public int ID_AN { get; set; }
    public string TITLE_OR { get; set; }
    public string TITLE_EN { get; set; }

    public virtual ICollection<GENRES> GENRES { get; set; } // Use ICollection here
}

这篇关于不包含定义&QUOT;新增]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆