我有保存图像文件上传的代码如下,我在编辑表单中有困难,如何更改上传的图像?应该在编辑功能中添加什么代码?
public class EmployeeModel
{
[ScaffoldColumn(false)]
public int EmployeeID { get; set; }
[Required(ErrorMessage = "Please Enter Position ID")]
public int PositionID { get; set; }
[Required(ErrorMessage = "Please Enter NO PEK")]
public string NoPEK { get; set; }
public string AvatarUrl { get; set; }
}
[HttpPost]
public ActionResult CreateEmployee(EmployeeModel org, string Command, HttpPostedFileBase AvatarUrl)
{
if (!ModelState.IsValid)
{
return PartialView("_CreateEmployee", org);
}
else
{
Employee orgObj = new Employee();
orgObj.PositionID = org.PositionID;
orgObj.NoPEK = org.NoPEK;
orgObj.AvatarUrl = org.AvatarUrl;
var image = WebImage.GetImageFromRequest();
var filename = Path.GetFileName(image.FileName);
var path = Path.Combine(Server.MapPath("~/Content/upload"), filename);
image.Save(path);
orgObj.AvatarUrl = Url.Content(Path.Combine("/Content/upload", filename));
bool IsSuccess = mobjModel.AddEmployee(orgObj);
if (IsSuccess)
{
TempData["OperStatus"] = "Employee added succeessfully";
ModelState.Clear();
return RedirectToAction("IndexEmployee", "Employees");
}
return PartialView("_CreateEmployee");
}
}
[HttpPost]
public ActionResult UpdateEmployee(EmployeeModel org, string Command)
{
if (!ModelState.IsValid)
{
return PartialView("_EditEmployee", org);
}
else
{
Employee orgObj = new Employee();
orgObj.EmployeeID = org.EmployeeID;
orgObj.PositionID = org.PositionID;
orgObj.NoPEK = org.NoPEK;
orgObj.AvatarUrl = org.AvatarUrl;
bool IsSuccess = mobjModel.UpdateEmployee(orgObj);
if (IsSuccess)
{
TempData["OperStatus"] = "Employee updated succeessfully";
ModelState.Clear();
return RedirectToAction("IndexEmployee", "Employees");
}
}
return PartialView("_EditEmployee");
}