I am using following function to edit the employee records.
public async Task<ActionResult> Edit([Bind(Include = "Id,EmployeeId,Name,FatherName,JoiningDate,EndDate,InitialSalary,CurrentSalary,CurrentAddress,PermanentAddress,ContactNumber,EmailId,Gender,DOB,DeptId,DigId,PFNo,BranchCode,Qualification")] Employee employee)
{
if (ModelState.IsValid)
{
string fileName = null;
if (Request.Files["ImageFileToUpload"]!=null)
{
///Saving the file to EmployeeImages folder with unique name.
HttpPostedFileBase file = Request.Files["ImageFileToUpload"];
fileName = UploadEmployeeImage(file);
}
else
{
///what condition I need to write here so that if no image selected then it will not update the image field?
///if I am writing
fileName = db.Employees.Find(employee.Id).PhotoPath;
///it’s showing error.
}
employee.PhotoPath = fileName;
db.Entry(employee).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.DeptId = new SelectList(db.Departments, "DeptId", "DeptName", employee.DeptId);
ViewBag.DigId = new SelectList(db.Designations, "DegId", "DegName", employee.DigId);
ViewBag.BranchCode = new SelectList(db.Branches, "BranchId", "BranchName", employee.BranchCode);
return View(employee);
}
I want to update the image field when I select image otherwise the employee image should not be changed but other records may change.
Please suggest what I need to update in my code.