public IActionResult AddBrand(AddBrandModel model) { if (ModelState.IsValid) { Brand brand = new Brand() { BrandName = model.BrandName }; if (model.BrandLogo != null) { var fileName = Path.Combine(hostingEnvironment.WebRootPath, "images/BrandLogo", Path.GetFileName(model.BrandLogo.FileName)); model.BrandLogo.CopyTo(new FileStream(fileName, FileMode.Create)); brand.BrandLogo = Path.GetFileName(model.BrandLogo.FileName); unitOfWork.Brands.Add(brand); unitOfWork.SaveChanges(); List <Brand> brands = unitOfWork.Brands.GetAll() .Include(d => d.Products).ToList(); return(View("BrandList", brands)); } } ModelState.AddModelError("", "Brand adding is unsuccess"); return(RedirectToAction("BrandList")); }
public ActionResult Create(AddBrandModel brand) { // var errors = ModelState //.Where(x => x.Value.Errors.Count > 0) //.Select(x => new { x.Key, x.Value.Errors }) //.ToArray(); if (ModelState.IsValid) { try { // Insert Product Brand objnewbrand = new Brand(); objnewbrand.BRND_id = (db.Brands.Max(i => (int?)i.BRND_id) ?? 0) + 1; //Convert.ToInt32(db.PRODUCTs.Max(x => x.Pid)) + 1; objnewbrand.BRND_Name = brand.BRND_Name; objnewbrand.BRND_Description = brand.BRND_Description; objnewbrand.IsActive = true; objnewbrand.cat_id = brand.Category_id; objnewbrand.sub_cat_id = brand.Sub_Category_id; objnewbrand.BRND_Img_Path = string.Empty; if (brand.BRND_Img_Path != null) { string ImageName = System.IO.Path.GetFileName(brand.BRND_Img_Path.FileName); string physicalPath = Server.MapPath(ConfigurationManager.AppSettings["Mnu_ImgPath"] + ImageName); brand.BRND_Img_Path.SaveAs(physicalPath); //string targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); //brand.BRND_Img_Path.SaveAs(targetPath); objnewbrand.BRND_Img_Path = ImageName; objnewbrand.Is_Add_img = true; } objnewbrand.Add_User_id = WebSecurity.CurrentUserId; objnewbrand.Mod_User_Id = WebSecurity.CurrentUserId; objnewbrand.Add_Datetime = System.DateTime.Now; objnewbrand.Modify_Datetime = System.DateTime.Now; db.Brands.Add(objnewbrand); db.SaveChanges(); TempData["brnd_Message"] = ConfigurationManager.AppSettings["INS_SUC"]; ModelState.Clear(); } catch (Exception exception) { } return RedirectToAction("Create", "Brand"); } return View(brand); }