public ActionResult SaveStaff(StaffManagementViewModel model)
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
//string result = "Error Staff Info is not saved!";
var result = false;
try
{
tblCompany comp = new tblCompany();
if (model.tblCompany.CompanyId > 0)
{
comp = db.tblCompanies.Where(a => a.CompanyId == model.tblCompany.CompanyId).FirstOrDefault();
}
{
comp.DateJoin = model.tblCompany.DateJoin;
comp.Department = model.tblCompany.Department;
comp.JobTitle = model.tblCompany.JobTitle;
if (model.tblCompany.CompanyId <= 0)
db.tblCompanies.Add(comp);
db.SaveChanges();
model.CompanyId = comp.CompanyId;
}
tblStaff staff = new tblStaff();
if (model.StaffId > 0)
{
staff = db.tblStaffs.Where(a => a.StaffId == model.StaffId).FirstOrDefault();
}
staff.StaffNo = model.StaffNo;
staff.Picture =Picture ;
staff.FName = model.FName;
staff.LName = model.LName;
staff.BirthDate = model.BirthDate;
staff.PhoneNo = model.PhoneNo;
staff.Email = model.Email;
staff.FirstName = model.FirstName;
staff.LastName = model.LastName;
staff.PhoneNumb = model.PhoneNumb;
staff.CompanyId = model.CompanyId;
if (model.StaffId <= 0)
{
db.tblStaffs.Add(staff);
}
db.SaveChanges();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return Json(result, JsonRequestBehavior.AllowGet);
}

Vishal JoshiRank #377 · 6.7K reputationPosted Jan 23, 2024, 5:34 AM EST
Hello Emmanuel,
Either saving image to the database you can upload image to the server and save the relative path.
whenever require you can return relative path and it will show the image on the page.
Code Sample
Upload image to the server
public ActionResult FileUpload(HttpPostedFileBase file) { if (file != null) { string pic = System.IO.Path.GetFileName(file.FileName); string path = System.IO.Path.Combine( Server.MapPath("~/images/profile"), pic); // file is uploaded file.SaveAs(path); } // after successfully uploading redirect the user return View(path); }use the path to save to the database along with other data like name, type etc,
Thanks
Vishal
Sachin SinghRank #66 · 26.2K reputationPosted Jan 19, 2024, 11:32 AM EST
In the given code, i can't see where have you defined the picture variable.
Is it a byte[] or it is a string "image path"?
your complete code should be like this, where i am assuming that you are sending the picture as byte[] to controller and byte[] picture is defined as a property in your "StaffManagementViewModel" model
public ActionResult SaveStaff(StaffManagementViewModel model) { ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities(); var result = false; try { tblCompany comp = new tblCompany(); if (model.tblCompany.CompanyId > 0) { comp = db.tblCompanies.FirstOrDefault(a => a.CompanyId == model.tblCompany.CompanyId); } else { comp.DateJoin = model.tblCompany.DateJoin; comp.Department = model.tblCompany.Department; comp.JobTitle = model.tblCompany.JobTitle; if (model.tblCompany.CompanyId 0) { staff = db.tblStaffs.FirstOrDefault(a => a.StaffId == model.StaffId); } // Assign staff properties staff.StaffNo = model.StaffNo; staff.FName = model.FName; staff.LName = model.LName; staff.BirthDate = model.BirthDate; staff.PhoneNo = model.PhoneNo; staff.Email = model.Email; staff.FirstName = model.FirstName; staff.LastName = model.LastName; staff.PhoneNumb = model.PhoneNumb; staff.CompanyId = model.CompanyId; // Assuming model.Picture is a byte[] containing image data staff.Picture = model.Picture; if (model.StaffId <= 0) { db.tblStaffs.Add(staff); } db.SaveChanges(); result = true; } catch (Exception ex) { // Handle the exception appropriately, logging, returning error messages, etc. result = false; } return Json(result, JsonRequestBehavior.AllowGet); }Anandu G NathRank #681 · 3.5K reputationPosted Jan 25, 2024, 7:58 AM EST
public ActionResult FileUpload(HttpPostedFileBase file) { if (file != null) { string pic = System.IO.Path.GetFileName(file.FileName); string path = System.IO.Path.Combine( Server.MapPath("~/images/profile"), pic); file.SaveAs(path); } return View(path); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jan 24, 2024, 6:42 AM EST
Thank you team for your support