public IActionResult Edit(int id)//Id Az asp-route-id miad inja { PromotionAddEdit model = new PromotionAddEdit(); //Dropdown List - Products model.Products = _context.products.Select(c => new SelectListItem { Text = c.ProductName, Value = c.ProductId.ToString() }).ToList(); if (id != 0) { //Dependency Injection using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Promotion promotion = _context.promotion.Where(p => p.PromotionId == id).SingleOrDefault(); if (promotion != null) { model.ProductId = promotion.ProductId; model.PromotionId = promotion.PromotionId; } } } return(View("Add", model)); }
public IActionResult Add() { PromotionAddEdit model = new PromotionAddEdit(); //Dropdown List - Products model.Products = _context.products.Select(c => new SelectListItem { Text = c.ProductName, Value = c.ProductId.ToString() }).ToList(); return(View("Add", model)); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> Add(int PromotionId, PromotionAddEdit model, IEnumerable <IFormFile> files) { if (ModelState.IsValid) { /***********************/ if (PromotionId == 0) { //Insert using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Promotion promotionModel = AutoMapper.Mapper.Map <PromotionAddEdit, Promotion>(model); db.promotion.Add(promotionModel); db.SaveChanges(); } return(Json(new { status = "success", message = "پیشنهاد شگفتانگیز با موفقیت ثبت شد" })); } else { //Update using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Promotion promotionModel = AutoMapper.Mapper.Map <PromotionAddEdit, Promotion>(model); db.promotion.Update(promotionModel); db.SaveChanges(); } return(Json(new { status = "success", message = "پیشنهاد شگفتانگیز با موفقیت ویراش شد" })); } } //Dropdown List - Products model.Products = _context.products.Select(c => new SelectListItem { Text = c.ProductName, Value = c.ProductId.ToString() }).ToList(); //Display Validation with Jquery Ajax var list = new List <string>(); foreach (var validation in ViewData.ModelState.Values) { list.AddRange(validation.Errors.Select(error => error.ErrorMessage)); } return(Json(new { status = "error", error = list })); }