public ActionResult Edit(MhmCap input)
        {
            db.ContextOptions.LazyLoadingEnabled   = false;
            db.ContextOptions.ProxyCreationEnabled = false;

            var cap   = db.MhmCaps.Include(f => f.Countries).Include(f => f.Funds).Single(f => f.Id == input.Id);
            var entry = db.ObjectStateManager.GetObjectStateEntry(cap);

            db.ApplyCurrentValues <MhmCap>(entry.EntitySet.Name, input);

            NewMethod(input.cIds, input.fIds, cap);

            cap.UpdatedAt = DateTime.Now;
            cap.UpdatedBy = this.Permissions.User.Id;


            if (ModelState.IsValid)
            {
                try
                {
                    var rowsUpdated = db.SaveChanges();
                    return(this.RedirectToIndex());
                }
                catch (Exception ex)
                {
                    _log.Error(ex, ex);
                    throw;
                }
            }
            FetchRelationships(cap);

            return(View("Edit", cap));
        }
        public ActionResult Create(MhmCap cap, IEnumerable <int> cIds, IEnumerable <int> fIds)
        {
            cap.UpdatedAt = DateTime.Now;
            cap.UpdatedBy = this.Permissions.User.Id;

            if (ModelState.IsValid)
            {
                try
                {
                    db.MhmCaps.AddObject(cap);
                    foreach (var country in cap.Countries)
                    {
                        db.ObjectStateManager.ChangeObjectState(country, System.Data.EntityState.Unchanged);
                    }

                    foreach (var fund in cap.Funds)
                    {
                        db.ObjectStateManager.ChangeObjectState(fund, System.Data.EntityState.Unchanged);
                    }
                    db.SaveChanges();
                    return(this.RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    _log.Error(ex, ex);
                    throw;
                }
            }
            FetchRelationships(cap);
            return(View("Edit", cap));
        }
        public ActionResult Create()
        {
            var cap = new MhmCap();

            FetchRelationships(cap);

            return(View("Edit", cap));
        }
        private void NewMethod(IEnumerable <int> cIds, IEnumerable <int> fIds, MhmCap cap)
        {
            if (cIds == null)
            {
                cap.Countries.Clear();
            }
            else
            {
                foreach (var country in cap.Countries.ToList())
                {
                    if (!cIds.Contains(country.Id))
                    {
                        cap.Countries.Remove(country);
                    }
                }

                foreach (var countryId in cIds)
                {
                    if (!cap.Countries.Any(f => f.Id == countryId))
                    {
                        var country = new Country()
                        {
                            Id = countryId
                        };
                        db.Countries.Attach(country);
                        cap.Countries.Add(country);
                    }
                }
            }

            if (fIds == null)
            {
                cap.Funds.Clear();
            }
            else
            {
                foreach (var fund in cap.Funds.ToList())
                {
                    if (!fIds.Contains(fund.Id))
                    {
                        cap.Funds.Remove(fund);
                    }
                }

                foreach (var fundId in fIds)
                {
                    if (!cap.Funds.Any(f => f.Id == fundId))
                    {
                        var fund = new Fund()
                        {
                            Id = fundId
                        };
                        db.Funds.Attach(fund);
                        cap.Funds.Add(fund);
                    }
                }
            }
        }
 private void FetchRelationships(MhmCap cap)
 {
     ViewBag.Countries = db.Countries.Select(f => new { Id = f.Id, Name = f.Name, Selected = f.MhmCaps.Any(ec => ec.Id == cap.Id) })
                         .OrderBy(f => f.Name)
                         .ToList().Select(c =>
                                          new SelectListItem()
     {
         Value    = c.Id.ToString(),
         Text     = c.Name,
         Selected = c.Selected
     });
     ViewBag.Funds = db.Funds.Select(f => new { Id = f.Id, Name = f.Name, Selected = f.MhmCaps.Any(ec => ec.Id == cap.Id) })
                     .OrderBy(f => f.Name)
                     .ToList().Select(c =>
                                      new SelectListItem()
     {
         Value    = c.Id.ToString(),
         Text     = c.Name,
         Selected = c.Selected
     });
     ViewBag.CurrencyId = new SelectList(db.Currencies.Select(f => new { Id = f.Id, Name = f.Name }), "Id", "Name", cap.CurrencyId);
 }