How to Edit and update an uploaded image in ASP.NET MVC : I want to edit my Uploaded Image in my **Edit.cshtml** page , but when I click my edit action there exist all data in my employee form but no image file and then I fail to change my image if necessary.

I use [MsSql Server][1] as database and there save only image name no path or image as I wish.

I take image as datatype `string` , not `byte[]`

My Code-
========

**Employee Model**

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Web;

    namespace MVC_Login_Registration.Models
    {
        public class EmployeeInfo
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            [Key]
            public int EmployeeID { get; set; }
            [Required(ErrorMessage = "Employee Name is Required"), Display(Name = "Name")]
            public string EmployeeName { get; set; }
            [Required(ErrorMessage = "Employee Bangla Name is Required"), Display(Name = "Bangla Name")]
            public string EmployeeBanglaName { get; set; }

            //public int DepartmentID { get; set; }
            ////[ForeignKey("DepartmentID")]
            //public virtual Department Department { get; set; }

            //public int DesignationID { get; set; }
            //public virtual Designation Designation { get; set; }

            [Required(ErrorMessage = "Department Name is Required")]
            public string Department { get; set; }
            [Required(ErrorMessage = "Designation is Required")]
            public string Designation { get; set; }
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
            public DateTime JoiningDate{ get; set; }

            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
            public DateTime DateOfBirth { get; set; }
            [Required(ErrorMessage = "BloodGroup is Required"), Display(Name = "Blood Group")]
            public  bloodGroup BloodGroup { get; set; }
            [Required(ErrorMessage = "Gender is Required")]      
            public gender Gender { get; set; }   
            [Required(ErrorMessage = "Father Name is Required"), Display(Name = "Father Name")]
            public string FatherName { get; set; }
            [Required(ErrorMessage = "Mother Name is Required"), Display(Name = "Mother Name")]
            public string MotherName { get; set; }
            [Required(ErrorMessage = "Husband/Wife Name is Required"), Display(Name = "Spouse Name")]
            public string SpouseName { get; set; }
            public string ChildrenNumber { get; set; }
            public string CardNumber { get; set; }
            [Required(ErrorMessage = "Contact No is Required"), DataType(DataType.PhoneNumber)]
            public string ContactNo { get; set; }
            [Required(ErrorMessage = "Email is Required"), DataType(DataType.EmailAddress), StringLength(200)]
            public string Email { get; set; }
            
            [Required(ErrorMessage = "HomeContactNo is Required"),DataType(DataType.PhoneNumber)]
            public string HomeContactNo { get; set; }
            [Required(ErrorMessage = "District Name is Required")]
            public string District { get; set; }
            [Required(ErrorMessage = "Thana is Required")]
            public string Thana { get; set; }
            [Required(ErrorMessage = "Village Name is Required")]
            public string Vill { get; set; }
            [Required(ErrorMessage = "Post Code is Required")]
            public string PostCode { get; set; }
            public string Image { get; set; }
            //public byte[] Image { get; set; }
            public bool IsActive { get; set; }
           
            public string AddedBy { get; set; }
            
            public DateTime AddedDate { get; set; }
            public string UpdateBy { get; set; }
         
            public DateTime UpdateDate { get; set; }
           

        }

      public enum gender
        {
            Male,
            Female,
            Other
        }

        public enum bloodGroup
        {
            A_positive,
            A_negative,
            B_positive,
            B_negative,
            AB_positive,
            AB_negative,
            O_positive,
            O_negative
        }
    }

**Employee Controller**


      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      using MVC_Login_Registration.Models;
      using System.Data.Entity;
      using System.Net;
      using System.Data.Entity.Infrastructure;
      using System.IO;
      using System.Threading.Tasks;

      namespace MVC_Login_Registration.Controllers
      {


          public class EmployeeController : Controller
          {
              UserDbContext edb = new UserDbContext();

              

              //
              // GET: /Employee/
              public ActionResult Index() => View(edb.employeeinfo.ToList());


              public ActionResult Create() => View();


              [HttpPost]

              public ActionResult Create(EmployeeInfo empinfo, HttpPostedFileBase image)
              {
                 

                  empinfo.Image = System.IO.Path.GetFileName(image.FileName);
                  empinfo.AddedBy = "mahbub";
                  empinfo.AddedDate = DateTime.Now;
                  empinfo.UpdateBy = "mahbub";
                  empinfo.UpdateDate = DateTime.Now;
                  empinfo.IsActive = true;


                  if (ModelState.IsValid)
                  {
                      edb.employeeinfo.Add(empinfo);
                      edb.SaveChanges();
                      image.SaveAs("C:/EmployeeImage/" + empinfo.EmployeeID.ToString() + empinfo.Image);
                      image.SaveAs(Server.MapPath(Url.Content("~/EmployeeImage/" + empinfo.EmployeeID.ToString() + empinfo.Image)));
                      return RedirectToAction("Index");
                      //ModelState.Clear();               
                      //ViewBag.Message = "Information Created Successfully...";
                  }

                 
                  return View(empinfo);
              }

             

              public ActionResult Edit() => View(edb.employeeinfo.ToList());
              [HttpGet]

              public ActionResult Edit(int? id)
              {

                  EmployeeInfo empinfo = edb.employeeinfo.Find(id);
                  return View(empinfo);
              }
              [HttpPost]
              [ValidateAntiForgeryToken]

              public ActionResult Edit(EmployeeInfo empinfo, HttpPostedFileBase image)
              {
                  if (ModelState.IsValid)
                  {
                      var model = edb.employeeinfo .Find(empinfo.EmployeeID );
                      string oldfilePath = model.Image ;
                      if (image != null && image .ContentLength > 0)
                      {
                          var fileName = Path.GetFileName(image .FileName);
                          string path = System.IO.Path.Combine(
                          Server.MapPath("~/EmployeeImage/"), fileName);
                          image.SaveAs(path);
                          model.Image  = "/EmployeeImage/" + image .FileName;
                          string fullPath = Request.MapPath("~" + oldfilePath);
                          if (System.IO.File.Exists(fullPath))
                          {
                              System.IO.File.Delete(fullPath);
                          }
                      }

                      model.Image  = empinfo .Image ;
                     edb.SaveChanges();
                      return RedirectToAction("Index");
                  }
                  return View(empinfo);
              }

              [HttpGet]
              public ActionResult Delete(int id)
              {
                 EmployeeInfo empinfo = edb.employeeinfo.Find(id);
                  return View(empinfo);
              }

              [HttpPost ActionName("Delete")]
              public ActionResult DeleteConfirm(int id)
              {
                  EmployeeInfo empinfoToDelete = edb.employeeinfo.Find(id);
                  edb.employeeinfo.Remove(empinfoToDelete);
                  edb.SaveChanges();
                  return RedirectToAction("Index");
              }

              // GET: Department/Details/5
              public ActionResult Details(int id=0)
              {
                  EmployeeInfo empinfo = edb.employeeinfo.Find(id);
                  return View(empinfo);
              }
          }
      }

And **create.cshtml**


      @model MVC_Login_Registration.Models.EmployeeInfo

      @{
          ViewBag.Title = "Create";
      }

    

                                <div class="form-group">
                                    <div class="col-lg-12 col-md-12 col-sm-12 col-sm-12">
                                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
                                            <div class="col-lg-12 col-md-12 col-sm-12 col-sm-12">
                                                @Html.LabelFor(model => model.EmployeeName)<br />
                                                @*<input type="EmployeeName" class="form-control" id="EmployeeName" placeholder="Enter Your Name">*@
                                                @Html.TextBox("EmployeeName", "", new { id = "EmployeeNameTextBox", @class = "form-control" })
                                                @Html.ValidationMessageFor(model => model.EmployeeName)
                                            </div>
                                           
                                        <div class="col-lg-4 col-md-4 col-sm-4 col-sm-12">
                                            <div class="col-lg-12 col-md-12 col-sm-12 col-sm-12">
                                                @Html.LabelFor(model => model.Image, new { @class = "control-label col-md-2" })
                                                <input type="file" name="image" onchange="previewFile()"><br>
                                                <img src="" height="100" alt="Image preview...">
                                                <script>
                                                    function previewFile() {
                                                        var preview = document.querySelector('img');
                                                        var file = document.querySelector('input[type=file]').files[0];
                                                        var reader = new FileReader();

                                                        reader.addEventListener("load", function () {
                                                            preview.src = reader.result;
                                                        }, false);

                                                        if (file) {
                                                            reader.readAsDataURL(file);
                                                        }
                                                    }
                                                </script>
                                                @Html.ValidationMessageFor(model => model.Image)

                                            </div>
                                            
                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 col-sm-12">
                            <input type="submit" value="Create" class="btn btn-success" />
                        </div>
                    </form>
                </div>
                <div>
                    @Html.ActionLink("Back to List", "Index")
                </div>
         

**Edit.cshtml**

  <div class="col-lg-4 col-md-4 col-sm-4 col-sm-12">
                                    <div class="col-lg-12 col-md-12 col-sm-12 col-sm-12">

                                        @Html.LabelFor(model => model.Image, new { @class = "control-label col-md-2" })
                                        <input type="file" name="image" onchange="previewFile()" onload="loadFile()"><br>
                                        <img src="../EmployeeImage/(item.EmployeeID.ToString()+item.Image)" height="100">
                                        <img src=model.Image height="100" alt="Image preview...">
                                        <script>
                                            function previewFile() {
                                                var preview = document.querySelector('img');
                                                var file = document.querySelector('input[type=file]').files[0];
                                                var reader = new FileReader();

                                                reader.addEventListener("load", function () {
                                                    preview.src = reader.result;
                                                }, false);

                                                if (file) {
                                                    reader.readAsDataURL(file);
                                                }
                                            }
                                           
                                        </script>
                                        @Html.ValidationMessageFor(model => model.Image)
                                        @Html.HiddenFor(model => model.Image)
                                    </div>

  [1]: https://www.microsoft.com/en-us/sql-server/sql-server-2016