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"); } }
// GET: Views/Create public ActionResult Create() { try { if (db.UserRight.FirstOrDefault(x => x.UserCode == User.Identity.Name).IsAdmin || db.UserRight.FirstOrDefault(x => x.UserCode == User.Identity.Name).IsEditor) { ViewBag.Lsystem = db.Lsystem.Where(x => x.LsystemFamilyID != 62); var model = new Views(); ViewBag.Calculation = db.Calculation.Where(x=>!x.CalculationFormula.Contains("Length")).ToList(); ViewBag.TC = db.TechnicalCharacteristic.OrderBy(x=>x.TCName).ToList(); return View(); } else return View("AuthorizationError"); } catch (Exception e) { ViewBag.Error = e.Message; return View("Error"); } }