public ActionResult Edit([Bind(Include = "ConfigCol,selected")] EditConfigVM editVM,int[] OptionValues)
        {
            try
            {
                var con12 = db.ConfigurationCollection.FirstOrDefault(x => x.ConfigurationCollectionID == editVM.ConfigCol.ConfigurationCollectionID);
                ViewBag.LsystemID = con12.LsystemID;
                if (ModelState.IsValid)
                {
                    ConfigurationCollection concol = db.ConfigurationCollection.FirstOrDefault(x => x.ConfigurationCollectionID == editVM.ConfigCol.ConfigurationCollectionID);
                    if (db.ConfigurationCollection.Any(x => x.CollectionName.Equals(editVM.ConfigCol.CollectionName) && x.ConfigurationCollectionID != editVM.ConfigCol.ConfigurationCollectionID))
                    {
                        ModelState.AddModelError("CollectionName", "Colelction Name already exists");
                        ConfigurationCollection configurationCollection = db.ConfigurationCollection.Find(editVM.ConfigCol.ConfigurationCollectionID);
                        var model = new EditConfigVM
                        {
                            ConfigCol = configurationCollection,
                            selected = new List<OptionValue>(),
                            Options = db.Option.Where(x => x.LsystemID == configurationCollection.LsystemID || x.LsystemID == 1038).ToList()
                        };
                        var sel = db.Config_OptionVal.Where(x => x.ConfigurationCollectionID == editVM.ConfigCol.ConfigurationCollectionID).ToList();
                        var Lsystem = db.Lsystem.FirstOrDefault(x => x.LsystemID == configurationCollection.LsystemID);
                        model.ConfigCol.OptionValues = new List<OptionValue>();
                        foreach (var item in sel)
                            model.selected.Add(db.OptionValue.FirstOrDefault(x => x.OptionValueID == item.OptionValueID));
                        ViewBag.LsystemID = new SelectList(db.Lsystem.OrderBy(x=>x.LsystemName), "LsystemID", "LsystemName", editVM.ConfigCol.LsystemID);
                        return View(editVM);
                    }
                    if (OptionValues == null)
                    {
                        ModelState.AddModelError("", "Configuration Cannot be empty. Select Values for Options");
                        ConfigurationCollection configurationCollection = db.ConfigurationCollection.Find(editVM.ConfigCol.ConfigurationCollectionID);
                        var model = new EditConfigVM
                        {
                            ConfigCol = configurationCollection,
                            selected = new List<OptionValue>(),
                            Options = db.Option.Where(x => x.LsystemID == configurationCollection.LsystemID || x.LsystemID == 1038).ToList()
                        };
                        var sel = db.Config_OptionVal.Where(x => x.ConfigurationCollectionID == editVM.ConfigCol.ConfigurationCollectionID).ToList();
                        var Lsystem = db.Lsystem.FirstOrDefault(x => x.LsystemID == configurationCollection.LsystemID);
                        model.ConfigCol.OptionValues = new List<OptionValue>();
                        foreach (var item in sel)
                            model.selected.Add(db.OptionValue.FirstOrDefault(x => x.OptionValueID == item.OptionValueID));
                        model.ConfigCol.Lsystem = Lsystem;
                        return View(model);
                    }
                    
                    var opl = db.Lsystem.FirstOrDefault(x => x.LsystemID == con12.LsystemID).Options.ToList();
                    var opcheck = new List<Option>();
                    foreach (var ov in OptionValues)
                        opcheck.Add(db.OptionValue.FirstOrDefault(x => x.OptionValueID == ov).Option);
                    bool unavailable = false;
                    foreach (var item in opl)
                        if (!opcheck.Contains(item))
                            unavailable = true;

                    if (unavailable)
                    {
                        ModelState.AddModelError("", "Option values for all Options in the system has not been selected");
                        ConfigurationCollection configurationCollection = db.ConfigurationCollection.Find(editVM.ConfigCol.ConfigurationCollectionID);
                        var model = new EditConfigVM
                        {
                            ConfigCol = configurationCollection,
                            selected = new List<OptionValue>(),
                            Options = db.Option.Where(x => x.LsystemID == configurationCollection.LsystemID || x.LsystemID == 1038).ToList()
                        };
                        var sel = db.Config_OptionVal.Where(x => x.ConfigurationCollectionID == editVM.ConfigCol.ConfigurationCollectionID).ToList();
                        var Lsystem = db.Lsystem.FirstOrDefault(x => x.LsystemID == configurationCollection.LsystemID);
                        model.ConfigCol.OptionValues = new List<OptionValue>();
                        foreach (var item in sel)
                            model.selected.Add(db.OptionValue.FirstOrDefault(x => x.OptionValueID == item.OptionValueID));
                        model.ConfigCol.Lsystem = Lsystem;
                        return View(model);
                    }

                    concol.ModifiedOn = DateTime.Now;
                    concol.ModifiedBy = User.Identity.Name;
                    concol.CollectionName = editVM.ConfigCol.CollectionName;
                    concol.DescriptionDE = editVM.ConfigCol.DescriptionDE;
                    concol.DescriptionEN = editVM.ConfigCol.DescriptionEN;
                    
                    db.Entry(concol).State = EntityState.Modified;
                    db.Entry(concol).Property(x => x.CreatedOn).IsModified = false;
                    db.Entry(concol).Property(x => x.CreatedBy).IsModified = false;
                    db.SaveChanges();
                    var dellist = db.Config_OptionVal.Where(x => x.ConfigurationCollectionID == editVM.ConfigCol.ConfigurationCollectionID).ToList();
                    foreach (var sel in dellist)
                        db.Config_OptionVal.Remove(sel);

                    foreach (var item in OptionValues)
                    {
                        var config_ov = new Config_OptionVal();
                        var con = concol;
                        var ov = db.OptionValue.Find(item);
                        config_ov.ConfigurationCollection = con;
                        config_ov.OptionValue = ov;
                        config_ov.ConfigurationCollectionID = con.ConfigurationCollectionID;
                        config_ov.OptionValueID = ov.OptionValueID;
                        config_ov.OptionID = ov.OptionID;
                        db.Config_OptionVal.Add(config_ov);
                        db.SaveChanges();

                    }
                    
                    return RedirectToAction("DisplayAll", "ConfigurationCollections", new { id= con12.LsystemID});
                }
                
                editVM.selected = new List<OptionValue>();
                editVM.Options = db.Option.Where(x=>x.LsystemID==editVM.ConfigCol.LsystemID||x.LsystemID==1038).ToList();
                return View(editVM);
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }
        // GET: ConfigurationCollections/Edit/5
        public ActionResult Edit(int? id)
        {
            if (db.UserRight.FirstOrDefault(x => x.UserCode == User.Identity.Name).IsAdmin || db.UserRight.FirstOrDefault(x => x.UserCode == User.Identity.Name).IsEditor)
            
            {


                if (id == null)
                {
                    ViewBag.Error = "A null parameter was passed to the function";
                    return View("Error");
                }
                ConfigurationCollection configurationCollection = db.ConfigurationCollection.Find(id);
               
                if (configurationCollection == null)
                {
                    ViewBag.Error = "The requested Configuration collection do not exist";
                    return View("Error");
                }
                var model = new EditConfigVM
                {
                    ConfigCol = configurationCollection,
                    selected = new List<OptionValue>(),
                    Options = db.Option.Where(x => x.LsystemID == configurationCollection.LsystemID || x.LsystemID == 1038).OrderBy(x=>x.OptionName).ToList()
                };
                var sel = db.Config_OptionVal.Where(x => x.ConfigurationCollectionID == id).ToList();
                var Lsystem = db.Lsystem.FirstOrDefault(x => x.LsystemID == configurationCollection.LsystemID);
                model.ConfigCol.OptionValues = new List<OptionValue>();
                foreach (var item in sel)
                    model.selected.Add(db.OptionValue.FirstOrDefault(x => x.OptionValueID == item.OptionValueID));
                //ViewBag.LsystemFamilyID = new SelectList(db.LsystemFamily, "LsystemFamilyID", "FamilyName");
                //ViewBag.LsystemID = new SelectList(db.Lsystem, "LsystemID", "LsystemName");

                ViewBag.LsystemID = configurationCollection.LsystemID;
                return View(model);
            }
            else
                return View("AuthorizationError");
        }