首页 > 解决方案 > 仅更新其他字段后,图像文件设置为 null

问题描述

当我只更新图像以外的其他字段时,我有一个奇怪的功能,其他字段(FirstNameLastName)更新成功,但是图像将其自身设置为 null,但是当我相对于其他字段(FirstNameLastName)选择它时,它更新成功. 所以我想要的是当我不更新图像时它保持原样而不将其自身设置为空。

这是我New.cshtm处理创建和编辑数据的文件:

<form asp-action="New" method="Post" asp-controller="Student" enctype="multipart/form-data">
    <div asp-validation-summary="All"></div>

    <input asp-for="Id" type="hidden"/>
    <input name="IsEditMode" id="IsEditMode" value="@ViewBag.IsEditMode" type="hidden"/>


    <div class="form-row">
        <label>Upload Photo</label>
        <input asp-for="ImageUrl" type="file" id="file"  name="file" class="form-control"/>

    </div>

    <div class="form-row">
        <div class="col">
            <label asp-for="FirstName"></label>
            <input asp-for="FirstName" class="form-control"/>
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
        <div class="col">
            <label asp-for="MiddleName"></label>
            <input asp-for="MiddleName" class="form-control"/>
            <span asp-validation-for="MiddleName" class="text-danger"></span>
        </div>
    </div>
</form>

然后这些是我的控制器 Student.cs用来更新字段的方法:

public IActionResult New(Student student, string IsEditMode, IFormFile file)
        {

            if (!ModelState.IsValid)
            {
                ViewBag.IsEditMode = IsEditMode;
                return View(student);
            }

            try
            {

                if (IsEditMode.Equals("false"))
                {

                    _studentRepository.Create(student);
                     UploadFile(file, student.Id);
                    _toastNotification.AddSuccessToastMessage("Student has been created successfully.");

                }

                else
                {
                    _studentRepository.Edit(student);
                     UploadFile(file, student.Id);
                    _toastNotification.AddSuccessToastMessage("Student has been edited successfully.");

                }

                return RedirectToAction(nameof(Index));
            }
            catch (Exception e)
            {

                return RedirectToAction(nameof(Index));
            }

        }

        public IActionResult Edit(int id)
        {

            try
            {

                ViewBag.IsEditMode = "true";

                var student = _studentRepository.GetSingleStudent(id);

                return View("New", student);
            }
            catch (Exception ex)
            {
                return Content("Could not find Pet");
            }

        }

        public void UploadFile(IFormFile file, long studentId)
        {
            var fileName = file.FileName;
            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images",fileName);

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fileStream);
            }

            var student = _studentRepository.GetSingleStudent(studentId);
            student.ImageUrl = fileName;
            _studentRepository.Edit(student);
        }

然后,这就是我在存储库中更新的方式:

然后在我的存储库中,这就是我更新字段的方式“:

public void Edit(Student student)
        {
            var existingStudent = _context.Students

                .FirstOrDefault(s => s.Id == student.Id);

            if (existingStudent != null)
            {
                // updating student.

                _context.Entry(existingStudent).CurrentValues.SetValues(student);
                _context.Entry(existingStudent).State = EntityState.Modified;
          }
}

Index.cshtml这是一个列表图像和名字和姓氏以及(操作按钮) EditDelete按钮:

<table class="table table-striped">
        <thead class="thead-dark">
        <tr>
            <td ><b>Student Picture</b></td>
            <td><b>FirstName</b></td>
            <td><b>LastName</b></td>
            <td colspan="2"> <b>Actions</b></td>
        </tr>
        </thead>
        <tbody>
        @foreach (var student in Model)
        {
            <tr>
                <td>@student.StudentRegNo</td>
                <td>
                    <div class="thumbnail">
                        <img src="/images/@student.ImageUrl" width="90" height="90"/>
                    </div>
                </td>
                <td>@student.FirstName</td>
                <td>@student.LastName</td>
                <d>
                    <td>
                        <a class="btn btn-warning" asp-action="Details" asp-controller="Student" asp-route-Id="@student.Id">Details</a>
                    </td>
                    <td>
                        <a class="btn btn-primary" asp-action="edit" asp-route-Id="@student.Id">Edit</a>
                    </td>
                    <td>
                        <a
                            class="btn btn-danger delete"
                            asp-route-Id="@student.Id"
                            asp-action="Delete">
                            Delete
                        </a>
                    </td>
                </d>
            </tr>
        }
        </tbody>
    </table>

编辑

这是根据@Raul 解决方案的当前逻辑,但它不起作用:

if (student.ImageUrl != null)
                {
                    _context.Entry(existingStudent).CurrentValues.SetValues(student);
                    _context.Entry(existingStudent).State = EntityState.Modified;
                }

                else
                {
                    _context.Entry(existingStudent).Property(x => x.ImageUrl).IsModified = false; 
                    _context.Entry(existingStudent).CurrentValues.SetValues(student);
                    _context.Entry(existingStudent).State = EntityState.Modified;
                }

标签: c#asp.net-coreentity-framework-coreupdating

解决方案


这两种情况可以分开考虑。更新字段而不更新图片时,文件为空,则需要将现有学生的ImageUrl分配给已发布的学生。

        //...
        try
        {
            if (IsEditMode.Equals("false"))
            {
                _studentRepository.Create(student);
                UploadFile(file, student.Id);
                _toastNotification.AddSuccessToastMessage("Student has been created successfully.");

            }
            else
            {
                //edit mode
                if(file == null)//Updating fields without updating the image.
                {
                    var existingStudent = _context.Students.FirstOrDefault(s => s.Id == student.Id);

                    if (existingStudent != null)
                    {
                        // updating student with previousImageUrl
                        student.ImageUrl = existingStudent.ImageUrl;
                        _context.Entry(existingStudent).CurrentValues.SetValues(student);
                        _context.Entry(existingStudent).State = EntityState.Modified;
                        _context.SaveChanges();
                    }
                }
                else//Updating the fields and the image
                {
                    _studentRepository.Edit(student);
                    UploadFile(file, student.Id);
                }

                _toastNotification.AddSuccessToastMessage("Student has been edited successfully.");

            }

            return RedirectToAction(nameof(Index));
        }
        catch (Exception e)
        {
            return RedirectToAction(nameof(Index));
        }
    }

    public void UploadFile(IFormFile file, long studentId)
    {
        var fileName = file.FileName;
        var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", fileName);

        using (var fileStream = new FileStream(path, FileMode.Create))
        {
            file.CopyTo(fileStream);
        }

        var student = _studentRepository.GetSingleStudent(studentId);
        student.ImageUrl = fileName;
        _studentRepository.Edit(student);
    }

   public void Edit(Student student)
    {
        var existingStudent = _context.Students

            .FirstOrDefault(s => s.Id == student.Id);

        if (existingStudent != null)
        {
            // updating student.

            _context.Entry(existingStudent).CurrentValues.SetValues(student);
            _context.Entry(existingStudent).State = EntityState.Modified;
            _context.SaveChanges();
       }
    }

推荐阅读