public IActionResult Create(Guid idp, Guid id) { var model = new Models.tbls.location.location(); var row = db.TblLocations.Find(id); if (row == null) { row = new web_db._Weighing.TblLocation() { }; } else { model = new Models.tbls.location.location { Code = row.Code, FkP = row.FkP, Id = row.Id, Kind = row.Kind, wight = row.Wight, Title = row.Title, OtcodeAnbar = row.OtcodeAnbar, ForProduct = row.ForProduct, isdell = false }; } if (idp == Guid.Empty) { idp = row.FkP ?? Guid.Empty; } var rowp = db.TblLocations.Find(idp); if (rowp != null) { model.FkP = idp; model.Kind = rowp.Kind + 1; model.pcode = rowp.Code; model.ptitle = rowp.Title; model.OtcodeAnbar = rowp.OtcodeAnbar; model.ForProduct = true; } else { model.Kind = 1; } if (model.Code == 0) { model.Code = (db.TblLocations.Where(a => (a.FkP ?? Guid.Empty) == idp).Max(a => (int?)a.Code) ?? 0) + 1; } return(View(model)); }
public IActionResult Create(Models.tbls.location.location model) { if (!ModelState.IsValid) { ViewBag.error = "ثبت انجام نشد - فیلد ها را درست پرکنید"; return(View(model)); } try { var row = db.TblLocations.Find(model.Id); if (row == null) { row = new web_db._Weighing.TblLocation(); if (model.FkP != null) { row.Kind = db.TblLocations.Find(model.FkP).Kind + 1; } else { row.Kind = 1; } row.Id = Guid.NewGuid(); row.FkP = model.FkP; /**/ //row.Code = (db.TblLocations.Where(a => (a.FkP ?? Guid.Empty) == idp).Max(a => (int?)a.Code) ?? 0) + 1; db.TblLocations.Add(row); } row.Code = model.Code; // row.ord = 0; row.Title = model.Title; row.Isdell = false; row.Wight = model.wight; row.ForProduct = model.ForProduct; row.OtcodeAnbar = model.OtcodeAnbar; if (row.Wight == 0) { row.Wight = null; } // row.CodeFull = row.Code.ToString(); { var z = db.TblLocations.FirstOrDefault(a => a.CodeFull == row.CodeFull); if (z != null && z.Id != row.Id) { ViewBag.error = "ثبت انجام نشد - کد تکراری میباشد "; return(View(model)); } } { row.CodeFull = row.Code.ToString(); var rowp = db.TblLocations.Find(row.FkP); if (rowp != null) { row.CodeFull = rowp.Code + "-" + row.CodeFull; if (rowp != null) { var rowpp = db.TblLocations.Find(rowp.FkP); if (rowpp != null) { row.CodeFull = rowpp.Code + "-" + row.CodeFull; } } } } db.SaveChanges(); Models.cl._ListLocation = db.TblLocations.OrderBy(a => a.Code).ToList(); ViewBag.txt = "ثبت انجام شد "; return(RedirectToAction(nameof(Index))); } catch (Exception e) { } ViewBag.error = "ثبت انجام نشد "; return(View(model)); }