Nicolas
Nicolas

Reputation: 4756

Inserting Many-To-Many data in a database

I'm trying to add data to a Many-To-Many relationship.

Here's my controller

[HttpPost]
public ActionResult AddSkillsPost()
{
    int Teacher_ID = Convert.ToInt32(Session["Teacher_ID"]);
    var SkillsArray = Request.Form["chk_group[]"];
    if(SkillsArray != null)
    {
        foreach(var skill in SkillsArray)
        {
            int Skill_ID = Convert.ToInt32(skill);

            var SkillToBeModified = (from s in db.Skills
                                     where s.ID == Skill_ID
                                     select new Skill { ID = s.ID, SkillName = s.SkillName, Teachers = s.Teachers });

            var Teacher = (from t in db.Teachers
                           where t.ID == Teacher_ID
                           select new Teacher { ID = t.ID, FirstName = t.FirstName, LastName = t.LastName,
                                                Email = t.Email, Campus = t.Campus, Skills = t.Skills });

            SkillToBeModified.Teachers.Add(Teacher);
            db.Entry(SkillToBeModified).State = EntityState.Modified;

            db.SaveChanges();
        }
    }
    return RedirectToAction("MyProfile");
}

So I'm trying to add a teacher to a certain skill. However, I'm having a problem with this line

SkillToBeModified.Teachers.Add(Teacher);

I get the error

'IQueryable' does not contain a definition for 'Teachers' and no extension method 'Teachers' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?)

I'm assuming this is a syntax mistake (something with the query?), but I can't figure out the correct way to write it.

Here's my model for Skills

[Table("Skills")]
public class Skill
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required]
    public string SkillName { get; set; }

    public virtual List<Teacher> Teachers { get; set; }
    public Skill()
    {
        this.Teachers = new List<Teacher>();
    }
}

and here's how my DB looks like

enter image description here

Looking forward to any reply's

EDIT: the form,

<form name="addSkillsForm" action="AddSkillsPost" method="post">
    @foreach (var skill in Model.Skills)
    {
        <input type="checkbox" name="chk_group[]" value="@skill.ID" />@skill.SkillName< br />
    }
    <input type="submit" value="Update Skills" />
</form>

I'm using a check box form where users can check multiple items, maybe I'm not handeling them coorectly in my POST?

Upvotes: 1

Views: 50

Answers (1)

JamieD77
JamieD77

Reputation: 13949

If your DB looks like the picture, you should be able to just do this.

[HttpPost]
public ActionResult AddSkillsPost()
{
    int teacher_ID = Convert.ToInt32(Session["Teacher_ID"]);
    var SkillsArray = Request.Form["chk_group[]"];
    if (SkillsArray != null)
    {
        foreach (var skill in SkillsArray.Split(','))
        {
            int skill_ID = Convert.ToInt32(skill);
            db.TeacherSkills.Add(new TeacherSkill() { Teacher_ID = teacher_ID, Skill_ID = skill_ID });
            db.SaveChanges();
        }
    }
    return RedirectToAction("MyProfile");
}

however if you're adding teacher to a skill then you'd do this.

[HttpPost]
public ActionResult AddSkillsPost()
{
    var teacher_ID = Convert.ToInt32(Session["Teacher_ID"]);

    var SkillsArray = Request.Form["chk_group[]"];
    if (SkillsArray != null)
    {
        var teacher = db.Teachers.Find(teacher_ID);
        foreach (var skill in SkillsArray.Split(','))
        {
            var skill_ID = Convert.ToInt32(skill);
            var skillToBeModified = db.Skills.Find(skill_ID);
            skillToBeModified.Teachers.Add(teacher);
            db.Entry(skillToBeModified).State = EntityState.Modified;
            db.SaveChanges();
        }
    }
    return RedirectToAction("MyProfile");
}

Upvotes: 2

Related Questions