zeeshan akram

zeeshan akram

  • 1.4k
  • 325
  • 18k

Img Path is Required error in mvc using EF

Feb 20 2020 4:16 AM
I am inserting the Product, Image, and Product Detail, Everything is Fine but when I insert image then I got the error is ImagePath is not inserting when I use extra table image then this error occur, if I use column imgpath in Product then it's working, I use extra table beacuase I want to insert multiple images that's why I use extra table. the error is: imgPath is Required.
when I use breakpoint I can see the image then the cursor will reach on save function I don't the values in image object. Any Expert can tell me where I am wrong . if you any easy solution kindly guide me. Image
 Image
  1. public partial class Image  
  2. {  
  3.   
  4.     public int imgID { getset; }  
  5.     public string ImgName { getset; }  
  6.     public bigint prodid{ getset; }  
  7. }  
Product Model
  1. public partial class Product  
  2. {  
  3.   
  4.     public long ProductID { getset; }  
  5.     public string PName { getset; }  
  6.     public string PDescription { getset; }  
  7. }  
Product Master Detail
  1. public partial class MasterDetail  
  2. {  
  3.     public int mDID { getset; }  
  4.   
  5.     public long ProductID { getset; }  
  6.     public string OS { getset; }  
  7.     public string SimType { getset; }  
  8.     public string Other { getset; }  
  9. }  
 Product Interface
  1. public interface IProduct  
  2. {  
  3.   
  4.     void Add_NewProduct(Product prod, Specification spec, Image pic);  
  5. }  
Product Concrete class
  1. public void Add_NewProduct(Product prod, Specification spec, Image pic)  
  2. {  
  3. try  
  4. {  
  5. _db.Products.Add(prod);  
  6. _db.Specifications.Add(spec);  
  7. _db.Images.Add(pic);  
  8. _db.SaveChanges();  
  9. }  
  10. catch (Exception)  
  11. {  
  12.   
  13. throw;  
  14. }  
  15. }  
Product Controller
  1. [HttpPost]  
  2. public ActionResult AddNewProducts(ProductViewModel prod, List file)  
  3. {  
  4. try  
  5. {  
  6. List PTlist = _IproductType.PTList();  
  7. ViewBag.Ptlist = new SelectList(PTlist, "PType_ID""P_Name");  
  8.   
  9. //Product Color List  
  10. List pColorList = _IProductColor.PColorlist();  
  11. ViewBag.pColor_List = new SelectList(pColorList, "C_ID""C_Name_OR_Code");  
  12.   
  13. List pSizeList = _ISize.pSizeList();  
  14. ViewBag.pSizeLists = new SelectList(pSizeList, "S_ID""S_Size");  
  15.   
  16.   
  17. string PathDB = string.Empty;  
  18.   
  19. foreach (HttpPostedFileBase files in file)  
  20. {  
  21. string filename = Path.GetFileName(files.FileName);  
  22. string _filename = DateTime.Now.ToString("yymmssff") + filename;  
  23. string extension = Path.GetExtension(files.FileName);  
  24. string path = Path.Combine(Server.MapPath("~/Upload/"), _filename);  
  25. PathDB = "~/Upload/" + _filename;  
  26. if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".jpg" || extension.ToLower() == ".png")  
  27. {  
  28. if (files.ContentLength <= 1000000)  
  29. {  
  30. img = new Image();  
  31. prod.ImgID = img.imgID;  
  32. prod.ImgPath = PathDB;  
  33. prod.ProductID = img.ProdID;  
  34.   
  35. }  
  36. else  
  37. ViewBag.sizemsg = "Size Limit accessed ";  
  38. }  
  39. else  
  40. ViewBag.fileformat = "File is not Format is not Correct";  
  41.   
  42. }  
  43.   
  44. Product pro = new Product();  
  45. pro.ProductID = prod.ProductID;  
  46. pro.PName = prod.PName;  
  47. pro.PDescription = prod.PDescription;  
  48.   
  49. Specification p_spec = new Specification();  
  50. p_spec.PSpecificationID = prod.PSpecificationID;  
  51. p_spec.ProductID = prod.ProductID;  
  52. p_spec.OS = prod.OS;  
  53. p_spec.DualSim = prod.DualSim;  
  54. p_spec.TouchScreen = prod.TouchScreen;  
  55. p_spec.Other = prod.Other;  
  56. _IProducts.Add_NewProduct(pro, p_spec, img);  
  57. ViewBag.message = "Record Saved Successfully";  
  58. ModelState.Clear();  
  59.   
  60. }  
  61. catch (Exception ex)  
  62. {  
  63.   
  64. throw ex;  
  65. }  
  66. return RedirectToAction("ProductsList");  
  67. }  
Product View
  1. @using (Html.BeginForm("AddNewProducts""Items", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal" }))  
  2. "tabimage" class="tab-pane">  
  3. class="mgtp-15 mgbt-xs-20"> Images  
  4.   
  5. class="vd_panel-menu">  
  6.  class="btn vd_btn vd_bg-blue btn-sm save-btn">class="fa fa-save append-icon"> Save Changes class="btn btn-default btn-sm" data-toggle="modal" data-target="#myModal">class="fa fa-times append-icon"> Cancel Changes
View
  1. @using (Html.BeginForm("AddNewProducts""Items", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal" }))    
  2. {      
  3.     <div id="tabimage" class="tab-pane">    
  4.        <h3 class="mgtp-15 mgbt-xs-20"> Images</h3>      
  5.          <div class="vd_panel-menu">    
  6.            <div> <a class="btn vd_btn vd_bg-blue btn-sm save-btn"><i class="fa fa-save append-icon"></i> Save Changes</a> <a class="btn btn-default btn-sm" data-toggle="modal" data-target="#myModal"><i class="fa fa-times append-icon"></i> Cancel Changes</a></div>    
  7.          </div>    
  8.          <div class="form-group">    
  9.             <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>    
  10.             <div class="col-lg-9">    
  11.                 <span class="btn vd_btn vd_bg-green fileinput-button">    
  12.                 <i class="glyphicon glyphicon-plus"></i> <span>Add files...</span>    
  13.                 <!-- The file input field used as target for the file upload widget -->    
  14.                 <input type="file" name="file" required="required" multiple id="file" />    
  15.                 </span>     
  16.             </div>    
  17.          </div>    
  18.    </div>  
  19. }
 
 
 
 
 
 

Answers (2)