C# 仅更新其他字段后,图像文件设置为空
当我只更新图像以外的其他字段时,其他字段(C# 仅更新其他字段后,图像文件设置为空,c#,asp.net-core,entity-framework-core,updating,C#,Asp.net Core,Entity Framework Core,Updating,当我只更新图像以外的其他字段时,其他字段(FirstName,LastName)成功更新,但图像将其自身设置为null,但当我针对其他字段(FirstName,LastName)选择它时,它成功更新。所以我想要的是,当我不更新图像时,它保持原样,而不将其自身设置为null 这是我的New.cshtm文件,它处理创建和编辑数据: <form asp-action="New" method="Post" asp-controller="Student" enctype="multipart/f
FirstName
,LastName
)成功更新,但图像将其自身设置为null,但当我针对其他字段(FirstName
,LastName
)选择它时,它成功更新。所以我想要的是,当我不更新图像时,它保持原样,而不将其自身设置为null
这是我的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>
然后,这就是我在存储库中更新的方式:
然后,在我的存储库中,这就是我更新字段的方式:
Index.cshtml
,这是一个列出图像、名字和姓氏以及(操作按钮)Edit
,Delete
按钮的列表:
<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>
在
Edit
student方法中,您可以将ImageUrl属性的IsModified
设置为false,该属性不会更新数据库中的图像字段:
public void Edit(Student student)
{
var existingStudent = _context.Students.FirstOrDefault(s => s.Id == student.Id);
if (existingStudent != null)
{
// updating student.
_context.Student.Attach(existingStudent);
_context.Entry(existingStudent).State = EntityState.Modified;
_context.Entry(existingStudent).Property(x => x.ImageUrl).IsModified=false;
_context.SaveChanges();
}
}
当然,您需要在此检查您的ImageUrl
逻辑。如果您得到一个新图像,那么您将相应地更新您的模型
编辑:
您可以将if-else条件合并如下:
if (student.ImageUrl != null)
{
_context.Student.Add(existingStudent);
_context.Entry(existingStudent).State EntityState.Modified;
//_context.Student.Update(existingStudent); //You can also use this. Comment out the upper two lines
_context.SaveChanges();
}
else
{
// updating student.
_context.Student.Attach(existingStudent);
_context.Entry(existingStudent).State = EntityState.Modified;
_context.Entry(existingStudent).Property(x => x.ImageUrl).IsModified=false;
_context.SaveChanges();
}
你可以单独考虑这两种情况。当更新字段而不更新图像时,文件为NULL,然后需要将现有学生的IVIULL添加到发布的学生。
//...
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();
}
}
您可以检查更新期间收到的图像是否为空,如果为空,则可以附加您的模型,如:\u context.Entry.attach(existingStudent);
,然后将图像URL
:\u context.Entry(existingStudent).State=EntityState.modified;\u context.Entry的修改字段设置为false(existingStudent).Property(x=>x.ImageUrl).IsModified=false;_context.SaveChanges();
Hey@RahulSharma,我实际上是想了解你的观点,但请你在编辑myvoid edit(学生)时将此作为答案
,我将非常感谢。看起来你删除了以前的问题并重新发布。不要这样做。不管怎样,答案都是一样的:停止使用CurrentValues.SetValues
。你已经得到了答案,你只是顽固地拒绝接受。嘿,劳尔,很抱歉,当我实施你的解决方案时,反应太晚了在它的工作,但事情是有两种情况,1.更新字段而不更新图像。2.更新字段和图像。现在的情况1您的解决方案工作,但它不适用于2。我试图想出一个逻辑,但没有工作。让我在这里分享我的逻辑。可能是最后一件事,这一行\u context.Entry.Attach(existingStudent);
,附件
未解决。我将非常感谢您的回复。对于第二种情况,您可以对您的existingStudent模型使用.Update方法。嘿,劳尔,我已经更新了我的问题,请检查底部有一个编辑部分,我当前的代码在那里。谢谢。嘿,劳尔,当我使用Update()时
就像\u context.Update(student);
一样,还是一样。在If
部分。嘿,劳尔,我想我在这一行的第一条评论中解释时犯了一些错误(现在场景1的解决方案有效,但是场景2的解决方案无效。我试图想出一个逻辑,但没有成功),实际上,您的解决方案适用于场景2,但不适用于场景1,因为这是图像设置为null
public void Edit(Student student)
{
var existingStudent = _context.Students.FirstOrDefault(s => s.Id == student.Id);
if (existingStudent != null)
{
// updating student.
_context.Student.Attach(existingStudent);
_context.Entry(existingStudent).State = EntityState.Modified;
_context.Entry(existingStudent).Property(x => x.ImageUrl).IsModified=false;
_context.SaveChanges();
}
}
if (student.ImageUrl != null)
{
_context.Student.Add(existingStudent);
_context.Entry(existingStudent).State EntityState.Modified;
//_context.Student.Update(existingStudent); //You can also use this. Comment out the upper two lines
_context.SaveChanges();
}
else
{
// updating student.
_context.Student.Attach(existingStudent);
_context.Entry(existingStudent).State = EntityState.Modified;
_context.Entry(existingStudent).Property(x => x.ImageUrl).IsModified=false;
_context.SaveChanges();
}
//...
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();
}
}