Can't remove last item from ICollection

I am trying to remove the last item from the collection, but it doesn't work. I am using MVC 4 and EF Code first. I generate all views and controllers and add a module with a checkbox that each checkbox is checked for one type collection object.

these are my models:

{
    public class Efekt
    {
        public int ID { get; set; }
        public string Symbol { get; set; }
        public string Typ { get; set; }

        [DataType(DataType.MultilineText)]
        public string Opis { get; set; }

        [DataType(DataType.MultilineText)]
        public string Odnosnik
        {
            get
            {
                (...)
            }

           // set;
        }

        public virtual ICollection<Obszarowe> EfektyObszarowe { get; set; }
        public virtual ICollection<Przedmiot> Przedmioty { get; set; }

        public Efekt(){

        }

        public Efekt(string symbol,string opis,string odnosnik){

           (...)
        }
    }
}


{
    public class Obszarowe
    {
        public int ID { get; set; }
        public string Symbol { get; set; }
        public string Typ { get; set; }

        [DataType(DataType.MultilineText)]
        public string Opis { get; set; }

        [DataType(DataType.MultilineText)]
        public string Odnosnik
        {
            get
            {
                (...)
            }

            // set;
        }

        public virtual ICollection<Efekt> EfektyKierunkowe { get; set; }

        public Obszarowe(){

        }

        public Obszarowe(string symbol, string opis, string odnosnik)
        {

            (...)
        }
    }
}

      

and editing functions in efektcontroller

 public ActionResult Edit(int id = 0)
        {
            Efekt efekt = db.Efekts.Find(id);
            if (efekt == null)
            {
                return HttpNotFound();
            }
            EfektyObszarowe(efekt);
            return View(efekt);
        }

        private void EfektyObszarowe(Efekt efekt)
        {
            var allObszarowe = db.EfektyObszarowe;
            var kierunkoweObszarowe = new HashSet<int>(efekt.EfektyObszarowe.Select(c => c.ID));
            var viewModel = new List<KierunkoweObszarowe>();
            foreach (var obszar in allObszarowe)
            {
                viewModel.Add(new KierunkoweObszarowe
                {
                    ID = obszar.ID,
                    Symbol = obszar.Symbol,
                    Wybrany = kierunkoweObszarowe.Contains(obszar.ID)
                });
            }
            ViewBag.Courses = viewModel;
        }

        //
        // POST: /Efekt/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id , string[] selectedCourses)
        {
            Efekt efekt = db.Efekts.Find(id);

            if (ModelState.IsValid)
            {
                UpdateObszarowe(selectedCourses, efekt);

                db.Entry(efekt).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            EfektyObszarowe(efekt);
            return View(efekt);
        }

        private void UpdateObszarowe(string[] selectedCourses, Efekt instructorToUpdate)
        {
            if (selectedCourses == null)
            {
                instructorToUpdate.EfektyObszarowe = new List<Obszarowe>();
                return;
            }

            var selectedCoursesHS = new HashSet<string>(selectedCourses);
            var instructorCourses = new HashSet<int>
                (instructorToUpdate.EfektyObszarowe.Select(c => c.ID));
            //var temp = db.Efekts.Find(instructorToUpdate.ID);
           ///////// Obszarowe ob;

            foreach (var course in db.EfektyObszarowe)
            {
                if (selectedCoursesHS.Contains(course.ID.ToString()))
                {
                 if (!instructorCourses.Contains(course.ID))
                {
                     instructorToUpdate.EfektyObszarowe.Add(course);
                /*ob = db.EfektyObszarowe.Find(Int32.Parse(course));
                        instructorToUpdate.EfektyObszarowe.Add(ob);
                */

                    }
                }
                else
                {
                    if (instructorCourses.Contains(course.ID))
                    {
                        instructorToUpdate.EfektyObszarowe.Remove(course);
                    }   
                }
            }
        }

      

When I unchecked the box that says "Obszarowe" it is ok, remove the item from the collection and I only see checked objects, but when I uncheck it, there is no difference :( sorry for my english

enter image description here

+3


source to share


1 answer


You do...

instructorToUpdate.EfektyObszarowe = new List<Obszarowe>();

      

But you get instructorToUpdate

with ...



Efekt efekt = db.Efekts.Find(id);

      

This means that the collection is EfektyObszarowe

not loaded and therefore not tracked for changes. Therefore, you need to load the collection when you receive Efekt

:

Efekt efekt = db.Efekts.Include(e => e.EfektyObszarowe).Single(e.ID == id);

      

+3


source







All Articles