public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subCategoryFromDb = await _db.SubCategories.SingleOrDefaultAsync(s => s.Id == id); if (subCategoryFromDb == null) { return(NotFound()); } SubCategoryVm subCategoryVm = new SubCategoryVm() { CategoryList = await _db.Categories. Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToListAsync(), SubCategoryList = await _db.SubCategories.Select(c => c.Name).Distinct().ToListAsync(), SubCategory = subCategoryFromDb }; return(View(subCategoryVm)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subCatFromDb = await _db.SubCategories.Include(s => s.Category).SingleOrDefaultAsync(s => s.Id == id); if (subCatFromDb == null) { return(NotFound()); } SubCategoryVm subCategoryVm = await GenerateSubCategoryViewModel(); subCategoryVm.SubCategory = subCatFromDb; // set the category in dropdown subCategoryVm.CategoryId = subCatFromDb.CategoryId; //subCategoryVm.SubCategory.Name = subCatFromDb.Name; //subCategoryVm.SubCategory.CategoryId = subCatFromDb.CategoryId; return(View(subCategoryVm)); }
public async Task <IActionResult> Create(SubCategoryVm model) { if (ModelState.IsValid) { var doesSubCategoryExists = await _db.SubCategories.Where(s => s.Name == model.SubCategory.Name).CountAsync(); var doesPairSubcategoryAndcategoryExists = await _db.SubCategories.Where (s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).CountAsync(); if (doesSubCategoryExists > 0 && model.IsNew) { // error StatusMessage = "Error : SubCategory alredy exists"; } else { if (doesSubCategoryExists == 0 && !model.IsNew) { StatusMessage = "Error : SubCategory does not exist"; } else { if (doesPairSubcategoryAndcategoryExists > 0) { StatusMessage = "Error: Category and Subcategory already exist"; } else { _db.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } // return View(subCategoryVm); } var categories = await _db.Categories.ToListAsync(); SubCategoryVm subCategoryVm = new SubCategoryVm { CategoryList = categories.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategories.OrderBy(s => s.Name).Select(s => s.Name).Distinct().ToListAsync(), StatusMessage = StatusMessage }; return(View(subCategoryVm)); }
internal static NeededSubCategoryVm FromModel(NeededSubCategory model) { return(new NeededSubCategoryVm { Id = model.Id, SubCategory = SubCategoryVm.FromModel(model.SubCategory), NeedLevel = model.NeedLevel }); }
public static SearchVm FromModel(Search search) { return new SearchVm { Category = CategoryVm.FromModel(search.Category), SubCategory = SubCategoryVm.FromModel(search.SubCategory), Keywords = search.Keywords, City = search.City, State = search.State, ZipCode = search.ZipCode }; }
public async Task <IActionResult> Edit(int id, SubCategoryVm model) { if (ModelState.IsValid) { var doesSubCategoryExists = await _db.SubCategories.Where(s => s.Name == model.SubCategory.Name).CountAsync(); var doesPairSubcategoryAndcategoryExists = await _db.SubCategories.Where (s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).CountAsync(); if (doesSubCategoryExists == 0) { StatusMessage = "Error: SubCategory does not exist. You can not add it here"; } else { if (doesPairSubcategoryAndcategoryExists > 0) { StatusMessage = "Error: Category and SubCategory alredy exists"; } else { var subCategoryFromDb = await _db.SubCategories.FindAsync(id); subCategoryFromDb.Name = model.SubCategory.Name; subCategoryFromDb.CategoryId = model.SubCategory.CategoryId; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } var categories = await _db.Categories.ToListAsync(); SubCategoryVm subCategoryVm = new SubCategoryVm { CategoryList = categories.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategories.OrderBy(s => s.Name).Select(s => s.Name).Distinct().ToListAsync(), StatusMessage = StatusMessage }; return(View(subCategoryVm)); }
public async Task <IActionResult> Edit(int id, SubCategoryVm subCategoryVm) { if (ModelState.IsValid) { var subCategoryExist = _db.SubCategories.Where(s => s.Name == subCategoryVm.SubCategory.Name).Count(); // заради SubCategoryVm където добавих CategoryId //var doesPairSubCategoryAndCategoryExist = _db.SubCategories // .Where(s => s.CategoryId == subCategoryVm.SubCategory.CategoryId && s.Name == subCategoryVm.SubCategory.Name).Count(); var doesPairSubCategoryAndCategoryExist = _db.SubCategories .Where(s => s.CategoryId == subCategoryVm.CategoryId && s.Name == subCategoryVm.SubCategory.Name).Count(); string sub = subCategoryVm.SubCategory.Name; string cat = subCategoryVm.SubCategory.Category.Name; if (subCategoryExist == 0) { this.StatusMessage = $"Error : {sub} is new. You can not add a new SubCategory here"; } else if (doesPairSubCategoryAndCategoryExist > 0) { this.StatusMessage = $"Error : subcategory {sub} and category {cat} aleredy exist"; } else { var subCategoryFromDb = _db.SubCategories.Find(id); subCategoryFromDb.Name = subCategoryVm.SubCategory.Name; subCategoryFromDb.CategoryId = subCategoryVm.CategoryId; //subCategoryFromDb.CategoryId = subCategoryVm.SubCategory.CategoryId; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryVm subCategory = await GenerateSubCategoryViewModel(); // update status message subCategory.StatusMessage = this.StatusMessage; return(View(subCategory)); }
public async Task <IActionResult> Create(SubCategoryVm model) { if (ModelState.IsValid) { var doesSubCategoryExists = await _db.SubCategories.Where(s => s.Name == model.SubCategory.Name).CountAsync(); var doesPairSubcategoryAndcategoryExists = await _db.SubCategories.Where (s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).CountAsync(); //if (doesSubCategoryExists > 0 && model.IsNew) //{ // StatusMessage = "Error : SubCategory alredy exists"; //} //if (!model.IsNew && doesSubCategoryExists == 0) //{ // StatusMessage = "Error : SubCategory does not exist"; //} if (!model.IsNew && doesPairSubcategoryAndcategoryExists == 0) { StatusMessage = "Error : SubCategory does not exist"; } else if (doesPairSubcategoryAndcategoryExists > 0) { StatusMessage = "Error: Category and Subcategory already exist"; } else { _db.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryVm subCategoryVm = await GenerateSubCategoryViewModel(); // update status message subCategoryVm.StatusMessage = this.StatusMessage; return(View(subCategoryVm)); }
public async Task <IActionResult> Create() { var categories = await _db.Categories.ToListAsync(); SubCategoryVm subCategoryVm = new SubCategoryVm { CategoryList = categories.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList(), SubCategory = new SubCategory(), SubCategoryList = await _db.SubCategories.OrderBy(s => s.Name).Select(s => s.Name).Distinct().ToListAsync(), }; // subCategoryVm.SelectedCategoryid = -6; subCategoryVm.CategoryList.Insert(0, new SelectListItem { Text = "--Select Category--", Value = "" }); return(View(subCategoryVm)); }
private async Task <SubCategoryVm> GenerateSubCategoryViewModel() { var categories = await _db.Categories.ToListAsync(); var subCategories = await _db.SubCategories.ToListAsync(); SubCategoryVm subCategoryVm = new SubCategoryVm() { SubCategory = new SubCategory(), CategorySelectList = categories.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }).ToList(), SubCategoryNamesList = subCategories.Select(s => s.Name).Distinct().ToList(), }; subCategoryVm.CategorySelectList.Insert(0, new SelectListItem { Text = "Select a Category", Value = string.Empty }); return(subCategoryVm); }
public async Task <IActionResult> Create() { SubCategoryVm subCategoryVm = await GenerateSubCategoryViewModel(); return(View(subCategoryVm)); }