Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用实体框架和ASP.NET MVC5更新记录_C#_Entity Framework 4_Asp.net Mvc 5 - Fatal编程技术网

C# 使用实体框架和ASP.NET MVC5更新记录

C# 使用实体框架和ASP.NET MVC5更新记录,c#,entity-framework-4,asp.net-mvc-5,C#,Entity Framework 4,Asp.net Mvc 5,我正在使用以下功能编辑员工记录 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,Qu

我正在使用以下功能编辑员工记录

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);
}
公共异步任务编辑([Bind(Include=“Id,EmployeeId,Name,FatherName,JoiningDate,EndDate,InitialSalary,CurrentSalary,CurrentAddress,PermanentAddress,ContactNumber,EmailId,Gender,DOB,DeptId,DigId,PFNo,BranchCode,Qualification”)]员工)
{
if(ModelState.IsValid)
{
字符串文件名=null;
if(Request.Files[“ImageFileToUpload”]!=null)
{
///使用唯一名称将文件保存到EmployeeImages文件夹。
HttpPostedFileBase file=Request.Files[“ImageFileToUpload”];
fileName=上传员工形象(文件);
}
其他的
{
///我需要在此处写入什么条件,以便在未选择图像时不会更新图像字段?
///如果我在写作
fileName=db.Employees.Find(employee.Id).PhotoPath;
///它显示出错误。
}
employee.PhotoPath=文件名;
db.Entry(employee.State=EntityState.Modified;
等待db.saveChangesSync();
返回操作(“索引”);
}
ViewBag.DeptId=新选择列表(db.Departments,“DeptId”,“DeptName”,employee.DeptId);
ViewBag.DigId=新的选择列表(数据库名称,“DegId”、“DegName”、employee.DigId);
ViewBag.BranchCode=新选择列表(db.branchs,“BranchId”,“BranchName”,employee.BranchCode);
返回视图(员工);
}
我想在选择图像时更新图像字段,否则不应更改员工图像,但其他记录可能会更改


请在我的代码中建议我需要更新的内容

您可以在选择图像后设置路径

     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();

我终于找到了问题的答案。下面是我用来解决问题的代码

 [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);
    }
[HttpPost]
[ValidateAntiForgeryToken]
[操作名称(“编辑”)]
公共异步任务编辑发布(int Id)
{
员工=新员工();
employee=db.Employees.FindAsync(Id).Result;
//if(ModelState.IsValid)
//{
字符串文件名=null;
if(Request.Files[“ImageFileToUpload”].ContentLength>0)
{
var file=Request.Files[“ImageFileToUpload”];
///使用唯一名称将文件保存到EmployeeImages文件夹。
如果(!string.IsNullOrEmpty(employee.PhotoPath))
{
删除EmployeeImage(employee.PhotoPath);
}
fileName=上传员工形象(文件);
TryUpdateModel(员工);
employee.PhotoPath=文件名;
}
其他的
{
TryUpdateModel(employee,null,null,新字符串[]{“PhotoPath”});
}

如果(employee.DigId我也这么做了,但在没有照片的情况下,它会插入空值。我已经更新了答案。还要注意,更新部分在if条件内。当文件名为空时,这不会更新任何记录。如果文件名为空,则我想要的是,只有文件名不应更改,其他记录应更改。换句话说,如果文件名为空,则名称为空,则只有好的名称不应修改。您可以再次检查吗?当用户上载照片时,这会起作用。但当没有上载照片时,这不会修改其他记录。我的要求是,如果有照片,我必须更新所有记录。如果没有照片,则修改其他记录,并保持图像路径不变。