首页 > 解决方案 > 当我们不更新 c# mvc 时如何检索旧的路径图像

问题描述

我正在处理更新表单,我的情况是我不想更新新图像我只想更新其他数据然后在我提交表单时提交“图像 URL”显示空值不显示图像的旧路径.我已经尝试过我正在分享的这段代码,请告诉我如何解决这个问题。只要给出代码提示,我就会解决这个问题。

模型

  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<ProductType> PTlist = _IproductType.PTList();  
                   ViewBag.Ptlist = new SelectList(PTlist, "PType_ID", "P_Name");  

                   //Product Color List  
                   List<P_Color> pColorList = _IProductColor.PColorlist();  
                   ViewBag.pColor_List = new SelectList(pColorList, "C_ID", "C_Name_OR_Code");  

                   List<P_Size> 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");  
           }  

看法

<div id="tabimage" class="tab-pane">  
                           <h3 class="mgtp-15 mgbt-xs-20"> Images</h3>  
                           <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>  

标签: c#asp.net-mvc

解决方案


推荐阅读