zeeshan akram

zeeshan akram

  • 1.3k
  • 325
  • 16.6k

retrieve old Path of image when we not update

Apr 9 2020 1:49 AM
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
  1. public class ProductViewModel{  
  2.    public string ImageUrl { getset; }  
  3.   
  4.         [NotMapped]  
  5.         public HttpPostedFileBase imageUpload { getset; }  
  6.   
  7.         public ProductViewModel()  
  8.         {  
  9.             ImageUrl = "~/Scripts/imageloading/image.jpg";  
  10.         }  
  11.   
c#
  1. public ActionResult AddOrEditProducts(ProductViewModel prod)  
  2.        {  
  3.            Product pro = new Product();  
  4.            ProductDetail p_spec = new ProductDetail();  
  5.            var result = new jsonMessage();  
  6.            try  
  7.            {  
  8.                List PTlist = _IproductType.PTList();  
  9.                ViewBag.Ptlist = new SelectList(PTlist, "PType_ID""P_Name");  
  10.   
  11.                //Product Color List  
  12.                List pColorList = _IProductColor.PColorlist();  
  13.                ViewBag.pColor_List = new SelectList(pColorList, "C_ID""C_Name_OR_Code");  
  14.   
  15.                List pSizeList = _ISize.pSizeList();  
  16.                ViewBag.pSizeLists = new SelectList(pSizeList, "S_ID""S_Size");  
  17.                if (prod.imageUpload != null)  // shows null here 
  18.                {  
  19.                        string filename = Path.GetFileName(prod.imageUpload.FileName);  
  20.                        string _filename = DateTime.Now.ToString("yymmssff") + filename;  
  21.                        string extension = Path.GetExtension(prod.imageUpload.FileName);  
  22.                        prod.ImageUrl= "~/upload/" + _filename;  
  23.                    if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".jpg" || extension.ToLower() == ".png")  
  24.                        {  
  25.                            if (prod.imageUpload.ContentLength <= 1000000)  
  26.                            {  
  27.                                prod.imageUpload.SaveAs(Path.Combine(Server.MapPath("~/upload/"), _filename));
  28.                                p_spec = new ProductDetail();  
  29.                                p_spec.ProductID = prod.ProductID;  
  30.                                p_spec.OS = prod.OS.Trim();  
  31.                                p_spec.DualSim = prod.DualSim.Trim();  
  32.                                p_spec.Camera = prod.Camera.Trim();  
  33.                                p_spec.TouchScreen = prod.TouchScreen.Trim();  
  34.                                p_spec.ScreenSize = prod.ScreenSize.Trim();  
  35.                                p_spec.ProcessorType = prod.ProcessorType.Trim();  
  36.                                p_spec.RAM = prod.RAM.Trim();  
  37.                                p_spec.InternalMemory = prod.InternalMemory.Trim();  
  38.                                p_spec.Wifi = prod.Wifi.Trim();  
  39.                                p_spec.BatteryLife = prod.BatteryLife.Trim();  
  40.                                p_spec.Other = prod.Other.Trim();  
  41.                                p_spec.PDescription = prod.PDescription.Trim();  
  42.                                p_spec.Model = prod.Model.Trim();  
  43.                                p_spec.Condition = prod.Condition.Trim();  
  44.                                p_spec.Discount = prod.Discount;  
  45.                                p_spec.ImageUrl = prod.ImageUrl;  
  46.   
  47.                            }  
  48.                            else  
  49.                                ViewBag.sizemsg = "Size Limit accessed ";  
  50.                        }  
  51.                        else  
  52.                            ViewBag.fileformat = "File is not Format is not Correct";  

  53.                    pro = new Product();  
  54.                    pro.ProductID = prod.ProductID;  
  55.                    pro.PName = prod.PName.Trim();  
  56.                    pro.ManificturedPrice = prod.ManificturedPrice;  
  57.                    pro.P_SizeID = prod.P_SizeID;  
  58.                    pro.P_Color_ID = prod.P_Color_ID;  
  59.                    pro.PType_ID = prod.PType_ID;  
  60.                    pro.UnitWeight = prod.UnitWeight;  
  61.                    pro.UnitInStock = prod.UnitInStock;  
  62.                }  
  63.   
  64.                if (prod.ProductID == 0)  
  65.                {  
  66.                    _IProducts.AddOrEditProducts(pro, p_spec);  
  67.                    result.Message= "Product has been saved success..";  
  68.                    result.Status = true;  
  69.                    ModelState.Clear();  
  70.                }  
  71.                else  
  72.                {  
  73.                    _IProducts.AddOrEditProducts(pro, p_spec);  
  74.                    result.Message = "Product Update Successfully";  
  75.                    result.Status = true;  
  76.                    ModelState.Clear();  
  77.                }  
  78.            }  
  79.            catch (DbEntityValidationException e)  
  80.            {  
  81.                foreach (var eve in e.EntityValidationErrors)  
  82.                {  
  83.                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",  
  84.                        eve.Entry.Entity.GetType().Name, eve.Entry.State);  
  85.                    foreach (var ve in eve.ValidationErrors)  
  86.                    {  
  87.                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",  
  88.                            ve.PropertyName, ve.ErrorMessage);  
  89.                    }  
  90.                    result.Message = "We are unable to process your request at this time. Please try again later.";  
  91.                    result.Status = false;  
  92.                }  
  93.            }  
  94.            return RedirectToAction("ProductsList");  
  95.        }  
 
view
  1. <div id="tabimage" class="tab-pane">  
  2.                            <h3 class="mgtp-15 mgbt-xs-20"> Imagesh3>  
  3.                            <div class="vd_panel-menu">  
  4.   
  5.                                <input type="submit" value="Save Changes" class="btn vd_btn vd_bg-blue btn-icon btn-sm save-btn  fa fa-save" id="btnSave" />  
  6.                                <button type="reset" value="Cancel" class="btn vd_btn vd_bg-blue btn-icon btn-sm save-btn  fa fa-save" id="" />  
  7.   
  8.                            div>  
  9.                            <div class="row">  
  10.                                <div class="form-group">  
  11.                                    <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>  
  12.                                    <div class="col-lg-9">  
  13.                                        <div class="col-lg-5">  
  14.                                            <span class="btn vd_btn vd_bg-green fileinput-button">  
  15.                                                <i class="glyphicon glyphicon-plus">i> <span>Add files...span>  
  16.                                                <input type="file" name="imageUpload" multiple id="imageUpload" onchange="ShowimahePreview(this,document.getElementById('previewImage'))" />  
  17.                                            span>  
  18.                                        div>  
  19.   
  20.                                        <div class="col-lg-4">  
  21.                                            <img src="@Url.Content(Model.ImageUrl)" alt="Alternate Text" height="150" weight="" id="previewImage" />  
  22.                                        div>  
  23.   
  24.                                    div>  
  25.                                div>  
  26.   
  27.   
  28.                            div>  
  29.                              
  30.                        div>