public async Task <IActionResult> AddEditField(int Id, AddEditFieldViewModel model, string redirectUrl) { if (ModelState.IsValid) { var query = (from f in _context.Fields join pcf in _context.ProductCategoryFields on f.Id equals pcf.IdFaild select new ProductFieldViewModel() { Type = f.Type, IdProduct = f.Id, IdCategory = pcf.IdCategory }).Where(x => x.Type == 4); if (Id == 0) { using (var db = _serviceProvider.GetRequiredService <ApplicationDbContext>()) { //model.Type = (short)model.SelecteFieldTypeId; int[] aa = model.IdCategory; if (model.IdSelectGroup == 0) { model.IdSelectGroup = -1; } Field fieldModel = AutoMapper.Mapper.Map <AddEditFieldViewModel, Field>(model); if (!CheckDuplicateDrp(model.IdCategory) || model.Type != 4) { db.Fields.Add(fieldModel); await db.SaveChangesAsync(); var select = _context.Fields.LastOrDefault(); int ss = select.Id; foreach (var item in aa) { ProductCategoryFields groupField = new ProductCategoryFields(); groupField.IdFaild = ss; groupField.IdCategory = item; db.ProductCategoryFields.Add(groupField); //Add to ProductFields ProductField pf = new ProductField(); var selectProducts = _context.Products.Where(x => x.IdCategory == item); foreach (var itemProduct in selectProducts) { pf.IdProduct = itemProduct.Id; pf.IdField = ss; pf.Value = null; db.ProductFields.Add(pf); } //Add to ProductFields } await db.SaveChangesAsync(); } else { TempData["Notif"] = Notification.ShowNotif("فیلد خودرد ها قبلا به این دسته بندی اضافه شده است", type: ToastType.red); return(PartialView("_Succefullyresponse", redirectUrl)); } } TempData["Notif"] = Notification.ShowNotif(MessageType.Add, type: ToastType.green); return(PartialView("_Succefullyresponse", redirectUrl)); } //Edit else { using (var db = _serviceProvider.GetRequiredService <ApplicationDbContext>()) { db.ProductCategoryFields.RemoveRange(_context.ProductCategoryFields.Where(x => x.IdFaild == Id)); if (model.ICategories != null) { } await db.SaveChangesAsync(); if (!CheckDuplicateDrp(model.IdCategory)) { var selectType = _context.Fields.AsNoTracking().Where(x => x.Id == Id).AsNoTracking().FirstOrDefault(); if (selectType.Type == 4 && model.Type != 4) { int idProductField = _context.ProductFields.Where(x => x.IdField == selectType.Id).AsNoTracking().FirstOrDefault().Id; db.ProductSelectedItems.RemoveRange(_context.ProductSelectedItems.Where(x => x.IdProductField == idProductField)); //await db.SaveChangesAsync(); } model.Type = (short)model.Type; if (model.IdSelectGroup == 0) { model.IdSelectGroup = -1; } int[] aa = model.IdCategory; Field fieldModel = AutoMapper.Mapper.Map <AddEditFieldViewModel, Field>(model); try { db.Fields.Update(fieldModel); } catch (Exception e) { throw; } foreach (var item in aa) { ProductCategoryFields groupField = new ProductCategoryFields(); groupField.IdFaild = Id; groupField.IdCategory = item; db.ProductCategoryFields.Add(groupField); } await db.SaveChangesAsync(); } else { TempData["Notif"] = Notification.ShowNotif("فیلد خودرد ها قبلا به این دسته بندی اضافه شده است", type: ToastType.red); return(PartialView("_Succefullyresponse", redirectUrl)); } } TempData["Notif"] = Notification.ShowNotif(MessageType.Edit, type: ToastType.blue); return(PartialView("_Succefullyresponse", redirectUrl)); } } if (Id == 0) { TempData["Notif"] = Notification.ShowNotif(MessageType.addError, type: ToastType.yellow); } else { TempData["Notif"] = Notification.ShowNotif(MessageType.editError, type: ToastType.yellow); } model.SelectGroupList = await _context.SelectGroups.ToListAsync(); //model.SelectGroupList = await _context.SelectGroups.Select(c => new SelectListItem() //{ // Text = c.Title, // Value = c.Id.ToString() //}).ToListAsync(); return(PartialView("AddEditField", model)); }
/// <summary> /// Initializes a new instance of the <see cref="ProductCategory" /> class. /// </summary> public ProductCategory() : base(CLASS_NAME) { mFields = new ProductCategoryFields(this); }