Raju Fodse

Raju Fodse

  • 1.1k
  • 244
  • 31.5k

Update Image in MVC

Dec 7 2019 1:50 AM
I have table named "Contacts" in this table I want to update PhotoID Filed so as new file will uploaded and path will replace in database against record. I done following Code for Controller and Edit View but I Get erro 
Object reference not set to an instance of an object.
 
  1. [HttpGet]  
  2.        public ActionResult EditContactProfile(int id)  
  3.        {  
  4.            if (id == 0)  
  5.            {  
  6.                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);  
  7.            }  
  8.            Contact contact = db.Contacts.Find(id);  
  9.            if (contact == null)  
  10.            {  
  11.                return HttpNotFound();  
  12.            }  
  13.            return View(contact);  
  14.        }  
  15.   
  16.        [HttpPost]  
  17.        public ActionResult EditContactProfile(Contact contact, HttpPostedFileBase ImageFile)  
  18.        {  
  19.            if (ModelState.IsValid)  
  20.            {  
  21.                if (ImageFile.ContentLength>0)  
  22.                {  
  23.                    string fileName = Path.GetFileNameWithoutExtension(ImageFile.FileName);  
  24.                    string extension = Path.GetExtension(ImageFile.FileName);  
  25.                    fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;  
  26.                    contact.PhotoID = "~/Uploads/UploadImages/" + fileName;  
  27.                    fileName = Path.Combine(Server.MapPath("~/Uploads/UploadImages/"), fileName);  
  28.                    ImageFile.SaveAs(fileName);  
  29.                }  
  30.                db.Entry(contact).State = EntityState.Modified;  
  31.                db.SaveChanges();  
  32.                return RedirectToAction("IndexContacts");  
  33.            }  
  34.            return View(contact);  
  35.        }  
And View is 
  1. @model EasyES.Models.Contact  
  2.   
  3. @{  
  4.     ViewBag.Title = "EditContact";  
  5. }  
  6. <head>  
  7.   
  8.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  9.     <script src="~/Scripts/jquery-3.3.1.min.js"></script>  
  10.     <script src="~/Scripts/bootstrap.min.js"></script>  
  11.     <script src="~/Scripts/jquery-ui-1.12.1.js"></script>  
  12.     <style>  
  13.         input,  
  14.         select,  
  15.         textarea {  
  16.             max-width: 100%; /*280px; Dafult Size*/  
  17.         }  
  18.     </style>  
  19.   
  20. </head>  
  21. <body>  
  22.     <div class="EasyViewDiv">  
  23.         <div class="form-group">  
  24.             <div class="row">  
  25.                 <div class="col-md-6">  
  26.                     <h1 style="color:#808080">Edit Contact Profile Photo</h1>  
  27.                 </div>  
  28.                 <div class="col-md-6">  
  29.   
  30.                 </div>  
  31.             </div>  
  32.         </div>  
  33.   
  34.         @using (Html.BeginForm())  
  35.         {  
  36.             @Html.AntiForgeryToken()  
  37.             @Html.HiddenFor(a => a.ContactID)  
  38.             @Html.HiddenFor(a => a.Name)  
  39.             @Html.HiddenFor(a => a.NickName)  
  40.             @Html.HiddenFor(a => a.ComapnyName)  
  41.             @Html.HiddenFor(a => a.Department)  
  42.             @Html.HiddenFor(a => a.Designation)  
  43.             @Html.HiddenFor(a => a.Email1)  
  44.             @Html.HiddenFor(a => a.Email2)  
  45.             @Html.HiddenFor(a => a.Contact1)  
  46.             @Html.HiddenFor(a => a.Contact2)  
  47.             @Html.HiddenFor(a => a.Contact3)  
  48.             @Html.HiddenFor(a => a.AddressHome)  
  49.             @Html.HiddenFor(a => a.AddressWorks)  
  50.             @Html.HiddenFor(a => a.WebAddress)  
  51.             @Html.HiddenFor(a => a.KeyWord)  
  52.             @Html.HiddenFor(a => a.Status)  
  53.   
  54.   
  55.             <div class="row">  
  56.                 <div class="col-md-6">  
  57.                     <div class="form-group">  
  58.                         @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  59.                          
  60.                         <div class="form-group">  
  61.                             @Html.LabelFor(model => model.Name, "Contact Name", htmlAttributes: new { @class = "control-label col-md-12" })  
  62.                             <div class="form-group col-md-12">  
  63.                                 @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })  
  64.                                 @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })  
  65.                             </div>  
  66.                         </div>  
  67.                         
  68.                     </div>  
  69.                 </div>  
  70.                 
  71.                 <div class="col-md-6">  
  72.                     <div class="form-group">  
  73.                         @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  74.                         <div class="form-group">  
  75.                             @Html.LabelFor(model => model.PhotoID, "UPload Photo", htmlAttributes: new { @class = "control-label col-md-12" })  
  76.                             <div class="form-group col-md-12">  
  77.                                 <div class="form-group col-md-9">  
  78.                                     <input type="file" id="FileStorage" name="ImageFile" />  
  79.                                     <p class="text-danger">Only PNG file with 500 kb </p>  
  80.                                 </div>  
  81.                             </div>  
  82.                         </div>  
  83.   
  84.                     </div>  
  85.                 </div>  
  86.   
  87.             </div>  
  88.   
  89.             <div align="center" class="container">  
  90.   
  91.                 <button type="submit" value="save" class="btn-outline-success ESbutton button3"><span class="fa fa-save"></span></button>  
  92.                 <button title="List View" class="btn-outline-info ESbutton button3" onclick="location.href='@Url.Action("IndexContacts", "Contacts")';return false;">  
  93.                     <span class="fa fa-list"></span>  
  94.                 </button>  
  95.             </div>  
  96.   
  97.   
  98.   
  99.   
  100.         }  
  101.     </div>  
  102. </body>  
 

Answers (5)