![](/img/trans.png)
[英]How to Update/Edit multiple records using Entity Framework in ASP.NET MVC
[英]Update records using Entity Framework and ASP.NET MVC5
我正在使用以下功能來編輯員工記錄。
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);
}
選擇圖像時,我想更新圖像字段,否則不應更改員工圖像,但其他記錄可能會更改。
請在我的代碼中建議我需要更新的內容。
終於我得到了我問題的解決方案。 以下代碼我用來解決我的問題。
[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);
}
選擇圖像后可以設置路徑。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.