public ActionResult Edit([Bind(Include = "Views12,TcSet")] EditViewsVM editVM, int[] TcSetID, int[]lsystemid, int[] CalcID)
        {
            ViewBag.Calculation = db.Calculation.Where(x => !x.CalculationFormula.Contains("Length")).ToList();

            if (ModelState.IsValid)
            {
                var views = db.Views.Where(x => x.ViewsID == editVM.Views12.ViewsID).FirstOrDefault();
                if(db.Views.Any(x=>x.ViewsName.Equals(editVM.Views12.ViewsName)&&(x.ViewsID!=editVM.Views12.ViewsID)))
                {
                    ModelState.AddModelError("", "View name already exists");
                    Views views12 = db.Views.Find(editVM.Views12.ViewsID);
                    var model = new EditViewsVM
                    {
                        Views12 = views12,
                        TcSet = new List<TcSet>(), Calculation = new List<Calculation>()
                    };
                    var set = db.ViewsProperty.Where(x => x.ViewsID == views12.ViewsID).ToList();
                    foreach (var item in set)
                        model.TcSet.Add(item.TcSet);
                    ViewBag.TC = db.TechnicalCharacteristic.OrderBy(x=>x.TCName).ToList();
                    model.Calculation = db.ViewsCalculation.Where(x => x.ViewsID == views.ViewsID).Select(x => x.Calculation).ToList();
                    
                    return View(model);
                }
                if (TcSetID == null)
                {
                    ModelState.AddModelError("", "You have not selected any properties in the View");
                    //ViewBag.Lsystem = db.Lsystem.Where(x => x.LsystemID != 0);
                    Views views12 = db.Views.Find(views.ViewsID);
                    var model = new EditViewsVM
                    {
                        Views12 = views12,
                        TcSet = new List<TcSet>(),
                        Calculation =  new List<Calculation>()
                    };
                    var set = db.ViewsProperty.Where(x => x.ViewsID == views12.ViewsID).ToList();
                    foreach (var item in set)
                        model.TcSet.Add(db.TcSet.FirstOrDefault(x => x.TcSetID == item.TcSetID));
                    ViewBag.TC = db.TechnicalCharacteristic.OrderBy(x=>x.TCName).ToList();
                    model.Calculation = db.ViewsCalculation.Where(x => x.ViewsID == views.ViewsID).Select(x => x.Calculation).ToList();


                    return View(model);
                }
                views.ViewsName = editVM.Views12.ViewsName;
                views.DescriptionDE = editVM.Views12.DescriptionDE;
                views.DescriptionEN = editVM.Views12.DescriptionEN;
                views.ModifiedBy = User.Identity.Name;
                views.ModifiedOn = DateTime.Now;
                
                
                db.Entry(views).State = EntityState.Modified;
                db.Entry(views).Property(x => x.CreatedBy).IsModified = false;
                db.Entry(views).Property(x => x.CreatedOn).IsModified = false;
                

               
                
                var viewprop = db.ViewsProperty.Where(x => x.ViewsID == views.ViewsID).ToList();
                foreach (var item in viewprop)
                    db.ViewsProperty.Remove(item);
                var setname = new List<string>();
                foreach(var setid in TcSetID)
                {
                    
                    var set = db.TcSet.FirstOrDefault(y => y.TcSetID == setid).SetName;
                    if(!setname.Contains(set))
                    {
                        setname.Add(set);
                        var x = new ViewsProperty();
                        x.TcSetID = setid;
                        x.ViewsID = views.ViewsID;
                        db.ViewsProperty.Add(x);
                    }
                   
                }

                foreach (var item in db.ViewsCalculation.Where(x => x.ViewsID == views.ViewsID).ToList())
                    db.ViewsCalculation.Remove(item);
                foreach(var item in CalcID)
                {
                    var c = new ViewsCalculation
                    {
                        ViewsID = views.ViewsID,
                        CalculationID = item

                    };
                    db.ViewsCalculation.Add(c);
                }

                db.SaveChanges();
                return RedirectToAction("Index");
            
            }
            return View("Error");
        }
        public ActionResult Create([Bind(Include = "ViewsID,ViewsName,DescriptionEN,DescriptionDE")] Views views, int[] lsystemid, int[] TcSetID, int[] CalcID)
        {            
            try
            {
                if (lsystemid != null)
                {
                    ViewBag.Lsystem = db.Lsystem.Where(x => x.LsystemFamilyID != 62);
                    var model = new Views { ViewsName= views.ViewsName };
                    var calcmodel = new List<Calculation>();
                    var tclist = new List<TechnicalCharacteristic>();
                    foreach (var item in lsystemid)
                    {
                        var op = db.Option.Where(x => x.LsystemID == item);
                        foreach (var opid in op)
                        {
                            int tcid = opid.TechnicalCharacteristicID;
                            if (!tclist.Contains(db.TechnicalCharacteristic.FirstOrDefault(x => x.TechnicalCharacteristicID == tcid)))
                                tclist.Add(db.TechnicalCharacteristic.FirstOrDefault(x => x.TechnicalCharacteristicID == tcid));
                            //if (!model.TechnicalCharacteristic.Any(x => x.TechnicalCharacteristicID == tcid))
                            //    model.TechnicalCharacteristic.Add(db.TechnicalCharacteristic.FirstOrDefault(x => x.TechnicalCharacteristicID == tcid));
                        }
                        var calcon = new CalculationsController();
                        var calclist = calcon.CalculationList(item);
                        foreach (var c in calclist)
                            calcmodel.Add(c);
                    }
                    //if ( == null)
                    //    ViewBag.Message = "No technical Characteristics to display";
                    
                    ViewBag.Calculation = calcmodel;
                    ViewBag.TC = tclist.OrderBy(x=>x.TCName).ToList();
                    return View(model);
                }
                if (ModelState.IsValid)
                {
                    if (db.Views.Any(x => x.ViewsName.Equals(views.ViewsName)))
                    {
                        ModelState.AddModelError("ViewsName", "View Name already exists");
                        ViewBag.Lsystem = db.Lsystem.Where(x => x.LsystemID != 0);
                        ViewBag.TC = db.TechnicalCharacteristic.OrderBy(x=>x.TCName).ToList();
                        ViewBag.Calculation = db.Calculation.Where(x => !x.CalculationFormula.Contains("Length")).ToList();
                        return View(views);
                    }
                    if (TcSetID == null)
                    {
                        ModelState.AddModelError("", "You have not selected any properties in the View");
                        ViewBag.Lsystem = db.Lsystem.Where(x => x.LsystemID != 0);
                        

                        return View(views);
                    }
                    views.CreatedBy = User.Identity.Name;
                    views.ModifiedBy = User.Identity.Name;
                    views.CreatedOn = DateTime.Now;
                    views.ModifiedOn = DateTime.Now;
                    //views.TechnicalCharacteristic = new List<TechnicalCharacteristic>();
                    var tcidlist = new List<int>();
                    foreach (var item in TcSetID)
                    {
                        tcidlist.Add(db.TcSet.FirstOrDefault(x => x.TcSetID == item).TechnicalCharacteristicID);

                    }
                    //foreach (var item in tcidlist.Distinct())
                    //    views.TechnicalCharacteristic.Add(db.TechnicalCharacteristic.FirstOrDefault(x => x.TechnicalCharacteristicID == item));

                    db.Views.Add(views);
                    db.SaveChanges();

                    foreach (var setid in TcSetID)
                    {
                        var x = new ViewsProperty();
                        x.TcSetID = setid;
                        x.ViewsID = views.ViewsID;
                        db.ViewsProperty.Add(x);
                    }

                    foreach(var cal in CalcID)
                    {
                        var c = new ViewsCalculation
                        {
                            CalculationID = cal,
                            Calculation = db.Calculation.FirstOrDefault(x => x.CalculationID == cal),
                            ViewsID = views.ViewsID,
                            Views = views
                        };
                        db.ViewsCalculation.Add(c);
                    }
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.Lsystem = db.Lsystem.Where(x => x.LsystemID != 0);
                ViewBag.Calculation = db.Calculation.Where(x => !x.CalculationFormula.Contains("Length")).ToList();
                ViewBag.TC = db.TechnicalCharacteristic.OrderBy(x=>x.TCName).ToList();
                //var model = new Views { TechnicalCharacteristic = db.TechnicalCharacteristic.ToList() };
                return View(views);
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }
        public ActionResult CopyView([Bind(Include = "ViewsID,ViewsName,DescriptionEN,DescriptionDE")] Views model, int CopyID)
        {
            try
            {


                if (ModelState.IsValid)
                {
                    if (db.Views.Any(x => x.ViewsName.Equals(model.ViewsName)))
                    {
                        ModelState.AddModelError("ViewsName", "Views name already Exists");
                        ViewBag.CopyID = new SelectList(db.Views.OrderBy(x=>x.ViewsName), "ViewsID", "ViewsName");
                        return View(model);
                    }
                    if (CopyID == 0)
                    {
                        ModelState.AddModelError("", "Select a View to be copied");
                        ViewBag.CopyID = new SelectList(db.Views.OrderBy(x=>x.ViewsName), "ViewsID", "ViewsName");
                        return View(model);
                    }
                    model.CreatedBy = User.Identity.Name;
                    model.CreatedOn = DateTime.Now;
                    model.ModifiedBy = User.Identity.Name;
                    model.ModifiedOn = DateTime.Now;

                    //model.TechnicalCharacteristic = new List<TechnicalCharacteristic>();
                    var copy = db.ViewsProperty.Where(x => x.ViewsID == CopyID).ToList();
                    //foreach (var item in copy)

                    //    model.TechnicalCharacteristic.Add(db.TcSet.FirstOrDefault(x => x.TcSetID == item.TcSetID).TechnicalCharacteristic);


                    db.Views.Add(model);
                    db.SaveChanges();

                    foreach (var item in copy)
                    {
                        ViewsProperty vp = new ViewsProperty
                        {
                            ViewsID = model.ViewsID,
                            TcSetID = item.TcSetID,
                            TcSet = db.TcSet.FirstOrDefault(x => x.TcSetID == item.TcSetID),
                            Views = db.Views.FirstOrDefault(x => x.ViewsID == model.ViewsID)
                        };


                        db.ViewsProperty.Add(vp);

                    }

                    foreach(var item in db.ViewsCalculation.Where(x=>x.ViewsID==CopyID).ToList())
                    {
                        ViewsCalculation vc = new ViewsCalculation
                        {
                             ViewsID = model.ViewsID,
                              CalculationID = item.CalculationID,
                               Views = db.Views.FirstOrDefault(x=>x.ViewsID==model.ViewsID),
                                Calculation = db.Calculation.FirstOrDefault(x=>x.CalculationID==item.CalculationID)
                        };
                        db.ViewsCalculation.Add(vc);
                    }
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.CopyID = new SelectList(db.Views.OrderBy(x=>x.ViewsName), "ViewsID", "ViewsName");
                return View();
            }
            catch(Exception e)
            {
                ViewBag.Error = e.Message;
                return View("Error");
            }
        }