Hello, I am working on update form, my scenario is I don't want to update new image I just want to update only other data then submit when I submit the form "image URL" shows the null value not show the old path of the image.
Any expert can you tell me how to correct this.
Model
- public class ProductViewModel{
- public string ImageUrl { get; set; }
-
- [NotMapped]
- public HttpPostedFileBase imageUpload { get; set; }
-
- public ProductViewModel()
- {
- ImageUrl = "~/Scripts/imageloading/image.jpg";
- }
-
- }
c#
- public ActionResult AddOrEditProducts(ProductViewModel prod)
- {
- Product pro = new Product();
- ProductDetail p_spec = new ProductDetail();
- var result = new jsonMessage();
- try
- {
- List PTlist = _IproductType.PTList();
- ViewBag.Ptlist = new SelectList(PTlist, "PType_ID", "P_Name");
-
-
- List pColorList = _IProductColor.PColorlist();
- ViewBag.pColor_List = new SelectList(pColorList, "C_ID", "C_Name_OR_Code");
-
- List pSizeList = _ISize.pSizeList();
- ViewBag.pSizeLists = new SelectList(pSizeList, "S_ID", "S_Size");
- if (prod.imageUpload != null) // shows null here
- {
- string filename = Path.GetFileName(prod.imageUpload.FileName);
- string _filename = DateTime.Now.ToString("yymmssff") + filename;
- string extension = Path.GetExtension(prod.imageUpload.FileName);
- prod.ImageUrl= "~/upload/" + _filename;
- if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".jpg" || extension.ToLower() == ".png")
- {
- if (prod.imageUpload.ContentLength <= 1000000)
- {
- prod.imageUpload.SaveAs(Path.Combine(Server.MapPath("~/upload/"), _filename));
- p_spec = new ProductDetail();
- p_spec.ProductID = prod.ProductID;
- p_spec.OS = prod.OS.Trim();
- p_spec.DualSim = prod.DualSim.Trim();
- p_spec.Camera = prod.Camera.Trim();
- p_spec.TouchScreen = prod.TouchScreen.Trim();
- p_spec.ScreenSize = prod.ScreenSize.Trim();
- p_spec.ProcessorType = prod.ProcessorType.Trim();
- p_spec.RAM = prod.RAM.Trim();
- p_spec.InternalMemory = prod.InternalMemory.Trim();
- p_spec.Wifi = prod.Wifi.Trim();
- p_spec.BatteryLife = prod.BatteryLife.Trim();
- p_spec.Other = prod.Other.Trim();
- p_spec.PDescription = prod.PDescription.Trim();
- p_spec.Model = prod.Model.Trim();
- p_spec.Condition = prod.Condition.Trim();
- p_spec.Discount = prod.Discount;
- p_spec.ImageUrl = prod.ImageUrl;
-
- }
- else
- ViewBag.sizemsg = "Size Limit accessed ";
- }
- else
- ViewBag.fileformat = "File is not Format is not Correct";
- pro = new Product();
- pro.ProductID = prod.ProductID;
- pro.PName = prod.PName.Trim();
- pro.ManificturedPrice = prod.ManificturedPrice;
- pro.P_SizeID = prod.P_SizeID;
- pro.P_Color_ID = prod.P_Color_ID;
- pro.PType_ID = prod.PType_ID;
- pro.UnitWeight = prod.UnitWeight;
- pro.UnitInStock = prod.UnitInStock;
- }
-
- if (prod.ProductID == 0)
- {
- _IProducts.AddOrEditProducts(pro, p_spec);
- result.Message= "Product has been saved success..";
- result.Status = true;
- ModelState.Clear();
- }
- else
- {
- _IProducts.AddOrEditProducts(pro, p_spec);
- result.Message = "Product Update Successfully";
- result.Status = true;
- ModelState.Clear();
- }
- }
- catch (DbEntityValidationException e)
- {
- foreach (var eve in e.EntityValidationErrors)
- {
- Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
- eve.Entry.Entity.GetType().Name, eve.Entry.State);
- foreach (var ve in eve.ValidationErrors)
- {
- Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
- ve.PropertyName, ve.ErrorMessage);
- }
- result.Message = "We are unable to process your request at this time. Please try again later.";
- result.Status = false;
- }
- }
- return RedirectToAction("ProductsList");
- }
view
- <div id="tabimage" class="tab-pane">
- <h3 class="mgtp-15 mgbt-xs-20"> Imagesh3>
- <div class="vd_panel-menu">
-
- <input type="submit" value="Save Changes" class="btn vd_btn vd_bg-blue btn-icon btn-sm save-btn fa fa-save" id="btnSave" />
- <button type="reset" value="Cancel" class="btn vd_btn vd_bg-blue btn-icon btn-sm save-btn fa fa-save" id="" />
-
- div>
- <div class="row">
- <div class="form-group">
- <label class="control-label col-lg-3 file_upload_label"> <span title="" data-toggle="tooltip" class="label-tooltip" data-original-title="Format JPG, GIF, PNG. Filesize 8.00 MB max."> Add a new image to this product span> label>
- <div class="col-lg-9">
- <div class="col-lg-5">
- <span class="btn vd_btn vd_bg-green fileinput-button">
- <i class="glyphicon glyphicon-plus">i> <span>Add files...span>
- <input type="file" name="imageUpload" multiple id="imageUpload" onchange="ShowimahePreview(this,document.getElementById('previewImage'))" />
- span>
- div>
-
- <div class="col-lg-4">
- <img src="@Url.Content(Model.ImageUrl)" alt="Alternate Text" height="150" weight="" id="previewImage" />
- div>
-
- div>
- div>
-
-
- div>
-
- div>