public ActionResult Create(ProductInsert product) { //Check server side validation using data annotation // 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 PRODUCT objnewproduct = new PRODUCT(); objnewproduct.Pid = (db.PRODUCTs.Max(i => (int?)i.Pid) ?? 0)+1; //Convert.ToInt32(db.PRODUCTs.Max(x => x.Pid)) + 1; objnewproduct.PDT_CODE = product.Product_Code; objnewproduct.PDT_Name = product.Product_Name; objnewproduct.PDT_Description = product.Product_Description; objnewproduct.PDT_Price = product.Product_Price; objnewproduct.Category_id = product.Category_id; objnewproduct.Sub_Category_id =Convert.ToInt32(product.Sub_Category_id); objnewproduct.offer_value_type_id = 1;// product.offer_id; objnewproduct.Brand_id = Convert.ToInt32(product.brand_id); objnewproduct.Add_user_Id = WebSecurity.CurrentUserId; objnewproduct.Mod_user_id = WebSecurity.CurrentUserId; objnewproduct.Add_Datetime = System.DateTime.Now; objnewproduct.Mod_Datetime = System.DateTime.Now; objnewproduct.IsActive = true; objnewproduct.PDT_offer = 1;// product.Product_offer; objnewproduct.PDT_Discount_price = 1;// product.offer_id == 1 ? (product.Product_Price - (product.Product_offer * product.Product_Price) / 100) : product.Product_Price - product.Product_offer; db.PRODUCTs.Add(objnewproduct); db.SaveChanges(); int productid = db.PRODUCTs.Where(x => x.PDT_CODE == product.Product_Code).FirstOrDefault().Pid; //insert PDT_IMAGE_PATH // save image in folder //image1 bool Firstflag = false; bool sideflag = false; bool vertiflag = false; bool horiflag = false; if (product.Firstflag == 1) { Firstflag = true; } if (product.Firstflag == 2) { sideflag = true; } if (product.Firstflag == 3) { horiflag = true; } if (product.Firstflag == 4) { vertiflag = true; } string ImageName = System.IO.Path.GetFileName(product.Imagepath.FileName); string physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.Imagepath.SaveAs(physicalPath); string targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.Imagepath.SaveAs(targetPath); PDT_IMAGE_PATH objImages = new PDT_IMAGE_PATH(); objImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objImages.PDT_ID = productid; objImages.Image_Path = ImageName; objImages.Home_Flag = product.Homeflag; objImages.First_Flag = Firstflag; objImages.Img_type = "F"; objImages.User_id = WebSecurity.CurrentUserId; objImages.His_Datetime = System.DateTime.Now; db.PDT_IMAGE_PATH.Add(objImages); //sideimage if (product.sideImagepath != null) { ImageName = System.IO.Path.GetFileName(product.sideImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.sideImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.sideImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objsideImages = new PDT_IMAGE_PATH(); objsideImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objsideImages.PDT_ID = productid; objsideImages.Image_Path = ImageName; objsideImages.First_Flag = sideflag; objsideImages.Img_type = "S"; objsideImages.User_id = WebSecurity.CurrentUserId; objsideImages.His_Datetime = System.DateTime.Now; db.PDT_IMAGE_PATH.Add(objsideImages); } //Horizontal image if (product.horizontalImagepath != null) { ImageName = System.IO.Path.GetFileName(product.horizontalImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.horizontalImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.horizontalImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objhorzImages = new PDT_IMAGE_PATH(); objhorzImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objhorzImages.PDT_ID = productid; objhorzImages.Image_Path = ImageName; objhorzImages.User_id = WebSecurity.CurrentUserId; objhorzImages.His_Datetime = System.DateTime.Now; objhorzImages.First_Flag = horiflag; objhorzImages.Img_type = "H"; db.PDT_IMAGE_PATH.Add(objhorzImages); } //vertical image if (product.verticalImagepath != null) { ImageName = System.IO.Path.GetFileName(product.verticalImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.verticalImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.verticalImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objvertImages = new PDT_IMAGE_PATH(); objvertImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objvertImages.PDT_ID = productid; objvertImages.Image_Path = ImageName; objvertImages.User_id = WebSecurity.CurrentUserId; objvertImages.His_Datetime = System.DateTime.Now; objvertImages.First_Flag = vertiflag; objvertImages.Img_type = "V"; db.PDT_IMAGE_PATH.Add(objvertImages); } //banner Image if (product.BannerImagepath != null) { ImageName = System.IO.Path.GetFileName(product.BannerImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.BannerImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.BannerImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objBannerImages = new PDT_IMAGE_PATH(); objBannerImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objBannerImages.PDT_ID = productid; objBannerImages.Image_Path = ImageName; objBannerImages.User_id = WebSecurity.CurrentUserId; objBannerImages.Img_type = "B"; objBannerImages.His_Datetime = System.DateTime.Now; objBannerImages.Banner_Flag = true; db.PDT_IMAGE_PATH.Add(objBannerImages); } db.SaveChanges(); //Specifications foreach (var p in product.GetType().GetProperties()) { // Console.WriteLine("Property {0} type {1} value {2}", p.Name, p.GetValue(obj, null).GetType().Name, p.GetValue(product, null)); if (db.KEY_Specifications.Any(u => u.spec_col_Name == p.Name)) { var propvalue = p.GetValue(product, null); if (propvalue != null) { Value_Specification objvaluespec = new Value_Specification(); objvaluespec.Spec_value_id = (db.Value_Specification.Max(i => (int?)i.Spec_value_id) ?? 0) + 1; objvaluespec.Product_id = productid; objvaluespec.Key_spec_id = db.KEY_Specifications.Where(x => x.spec_col_Name == p.Name).FirstOrDefault().spec_col_id; objvaluespec.key_Col_Value = propvalue.ToString(); objvaluespec.AddUser_id = WebSecurity.CurrentUserId; objvaluespec.mod_user_Id = WebSecurity.CurrentUserId; objvaluespec.Add_Datetime = System.DateTime.Now; objvaluespec.Mod_Datetime = System.DateTime.Now; db.Value_Specification.Add(objvaluespec); db.SaveChanges(); } } } TempData["Message"] = ConfigurationManager.AppSettings["INS_SUC"]; ModelState.Clear(); } catch(Exception exception ){ var queryError = from c in db.Menus where !(from o in db.menuTrees where o.Parent_Menuid != 0 select o.Menu_id) .Contains(c.Mnu_id) select new { c.Mnu_id, c.Mnu_Name }; ViewBag.Menus = new SelectList(queryError, "mnu_Id", "Mnu_Name", "--Select Category--"); ViewBag.Brands = new SelectList(db.Brands.Where(act => act.IsActive == true), "BRND_id", "BRND_Name", "--Select Brand--"); ViewBag.Sub_category = new SelectList(db.Spec_category, "spec_cat_id", "spec_cat_Name", "--Select Sub--"); ViewBag.offers = new SelectList(db.OFFER_VALUE_TYPE, "OFR_VAl_Type_Id", "Ofr_Value_type"); return View(product); } return RedirectToAction("Create", "Admin"); } var query = from c in db.Menus where !(from o in db.menuTrees where o.Parent_Menuid != 0 select o.Menu_id) .Contains(c.Mnu_id) select new { c.Mnu_id, c.Mnu_Name }; ViewBag.Menus = new SelectList(query, "mnu_Id", "Mnu_Name", "--Select Category--"); ViewBag.Brands = new SelectList(db.Brands.Where(act => act.IsActive == true), "BRND_id", "BRND_Name", "--Select Brand--"); ViewBag.Sub_category = new SelectList(db.Spec_category, "spec_cat_id", "spec_cat_Name", "--Select Sub--"); ViewBag.offers = new SelectList(db.OFFER_VALUE_TYPE, "OFR_VAl_Type_Id", "Ofr_Value_type"); return View(product); }
public ActionResult Addproduct(ProductInsert model) { //ViewBag.PossibleParadigms = _context.Paradigms; //viewModel.Entries = _context.Entries.ToList(); //FormCollection form //Check server side validation using data annotation if (ModelState.IsValid) { try { //ProductInsert model = new ProductInsert(); //// Insert Product //PRODUCT objnewproduct = new PRODUCT(); //objnewproduct.PDT_CODE = model["Product_Code"]; //objnewproduct.PDT_Name = model["Product_Name"]; //objnewproduct.PDT_Description = model["Product_Description"]; //objnewproduct.PDT_Price =Convert.ToDecimal(model["Product_Price"]); //objnewproduct.Category_id =int.Parse(model["Category_id"]); //objnewproduct.Sub_Category_id = int.Parse(model["Sub_Category_id"]); //objnewproduct.offer_id = int.Parse(model["offer_id"]); //objnewproduct.Brand_id = int.Parse(model["brand_id"]); //objnewproduct.Add_user_Id = WebSecurity.CurrentUserId; //objnewproduct.Mod_user_id = WebSecurity.CurrentUserId; //db.PRODUCTs.Add(objnewproduct); //db.SaveChanges(); //int productid = db.PRODUCTs.Where(x => x.PDT_CODE == model["Product_Code"]).FirstOrDefault().Pid; ////insert PDT_IMAGE_PATH // // save image in folder //string ImageName = System.IO.Path.GetFileName(model["Imagepath"].FileName); //string physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); //model["Imagepath"].SaveAs(physicalPath); //string targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); //model["Imagepath"].SaveAs(targetPath); //PDT_IMAGE_PATH objImages = new PDT_IMAGE_PATH(); //objImages.PDT_ID = productid; //objImages.Image_Path = ImageName; //objImages.Banner_Flag =Convert.ToBoolean(model["Bannerflag"]); //objImages.Home_Flag =Convert.ToBoolean(model["Homeflag"]); //objImages.First_Flag = Convert.ToBoolean(model["Firstflag"]); //objImages.User_id = WebSecurity.CurrentUserId; //db.PRODUCTs.Add(objnewproduct); int productid = 10; //Specifications foreach (var p in model.GetType().GetProperties()) { // Console.WriteLine("Property {0} type {1} value {2}", p.Name, p.GetValue(obj, null).GetType().Name, p.GetValue(model, null)); Value_Specification objvaluespec = new Value_Specification(); objvaluespec.Product_id = productid; objvaluespec.Key_spec_id = db.KEY_Specifications.Where(x => x.spec_col_Name == p.Name).FirstOrDefault().spec_col_id; if (objvaluespec.Key_spec_id != 0) { objvaluespec.key_Col_Value = p.GetValue(model, null).ToString(); } objvaluespec.AddUser_id = WebSecurity.CurrentUserId; objvaluespec.mod_user_Id = WebSecurity.CurrentUserId; } db.SaveChanges(); //Stream strm = model.ImagePhoto.InputStream; //var targetFile = targetPath; ////Based on scalefactor image size will vary //GenerateThumbnails(0.5, strm, targetFile); ////save new record in database //ImageDetail newRecord = new ImageDetail(); //newRecord.ImageId = Convert.ToInt32(db.ImageDetails.Max(x => x.ImageId)) + 1; //newRecord.ImageTitle = model.ImageTitile; //newRecord.ImageDescription = model.ImageDesc; //newRecord.CategoryId = model.CategoryId; //newRecord.ArtistId = model.ArtistId; //newRecord.ImagePath = ImageName; //newRecord.IsActive = true; //newRecord.ModifieBy = WebSecurity.CurrentUserId; //newRecord.CreatedBy = WebSecurity.CurrentUserId; //newRecord.CreatedDate = System.DateTime.Now; //newRecord.ModifiedDate = System.DateTime.Now; //newRecord.Price = model.Price; //db.ImageDetails.Add(newRecord); //db.SaveChanges(); ModelState.Clear(); } catch { } // return RedirectToAction("Display", "FileUpload"); } else { return View(); } return View(); }