1

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.

2 Answers 2

2

Finally i got the solution of my question. Following code i used to solve my problem.

 [HttpPost]
    [ValidateAntiForgeryToken]
    [ActionName("Edit")]
    public async Task<ActionResult> Edit_Post(int Id)
    {
        Employee employee = new Employee();
        employee = db.Employees.FindAsync(Id).Result;
        //if (ModelState.IsValid)
        //{
        string fileName = null;
        if (Request.Files["ImageFileToUpload"].ContentLength >0)
        {
            var file = Request.Files["ImageFileToUpload"];
            ///Saving the file to EmployeeImages folder with unique name.
            if (!string.IsNullOrEmpty(employee.PhotoPath))
            {
                DeleteEmployeeImage(employee.PhotoPath);                    
            }
            fileName = UploadEmployeeImage(file);
            TryUpdateModel(employee);
            employee.PhotoPath = fileName;
        }
        else
        {
            TryUpdateModel(employee, null, null, new string[] { "PhotoPath" });
        }
        if (employee.DigId <= 0)
        {
            ModelState.AddModelError("DigId", "Designation is required");
        }
        if (ModelState.IsValid)
        {
            db.Entry(employee).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        ViewBag.DeptIdList = new SelectList(db.Departments, "DeptId", "DeptName", employee.DeptId);
        ViewBag.DigIdList = new SelectList(db.Designations, "DegId", "DegName", employee.DigId);
        ViewBag.BranchCodeList = new SelectList(db.Branches, "BranchId", "BranchName", employee.BranchCode);
        return View(employee);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can set the path when you have selected an image.

     if (Request.Files["ImageFileToUpload"]!=null)
        {
            ///Saving the file to EmployeeImages folder with unique name.
            HttpPostedFileBase file = Request.Files["ImageFileToUpload"];
            fileName = UploadEmployeeImage(file);


           employee.PhotoPath = !string.isNullOrWhiteSpace(fileName) ? fileName : employee.PhotoPath ;


        }
        //else
        //{
         // else part not required.       
        //}
          db.Entry(employee).State = EntityState.Modified;
           await db.SaveChangesAsync();

6 Comments

i have done the same but it is inserting null value when there is no photo.
I have updated the answer. Also note that the Update part is inside if condition.
This will not update any record when fileName is null. What I want if file name is null then only file name should not change other record should change. In other words if file name is null then only fine name should not modify.
Can you check again ?
This is working when user upload the photo. But when no photo has been uploaded this will not modify other record. My requirement is i have to update all the record if photo is there if photo is not there then modify other record and leave the ImagePath as it is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.