如何修复基于 id 编辑页面的错误?
How can I fix error toedit pages based on id?
当我点击编辑按钮时出现错误,它在 img src 中显示错误(空引用)
这是成员控制器的编辑功能
public async Task<IActionResult> Edit(int memberId)
{
var memeber = await _context.Member.FindAsync(memberId); // Getting member by Id from database
return View(new MemberViewModel() { Member = memeber });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(MemberViewModel model, IFormFile photo/*int id, [Bind("MemberId,Name,Gender,DOB,MaritalStatus,Address,PhoneNo,Skills,Hobbies,JobTitle,Technology")] Member member*/)
{
if (photo == null || photo.Length == 0)
{
return Content("File not selected");
}
var path = Path.Combine(_webHostEnviroment.WebRootPath, "ImageName/Cover", photo.FileName);
using (FileStream stream = new FileStream(path, FileMode.Create))
{
await photo.CopyToAsync(stream);
stream.Close();
}
model.Member.ImageName = photo.FileName;
//Finding the member by its Id which we would update
var objMember = _context.Member.Where(mId => mId.MemberId == model.Member.MemberId).FirstOrDefault();
if (objMember != null)
{
//Update the existing member with new value
objMember!.Name = model.Member.Name;
objMember!.Gender = model.Member.Gender;
objMember!.DOB = model.Member.DOB;
objMember!.MaritalStatus = model.Member.MaritalStatus;
objMember!.Address = model.Member.Address;
objMember!.PhoneNo = model.Member.PhoneNo;
objMember!.Skills = model.Member.Skills;
objMember!.Hobbies = model.Member.Hobbies;
objMember!.JobTitle = model.Member.JobTitle;
objMember!.Technology = model.Member.Technology;
objMember!.ImageName = model.Member.ImageName;
objMember!.ImageLocation = path;
await _context.SaveChangesAsync();
}
return RedirectToAction("Index");
}
这是一个出现错误的视图
@model TeamManagement.ViewModel.MemberViewModel
<form asp-action="Edit" method="post" enctype="multipart/form-data">
<div class="row">
<div class="form-group col-md-4">
<label class="col-md-2 control-label">MemberPicture</label>
<div class="col-md-10">
<img src="~/ImageName/Cover/@Model.Member.ImageName"
class="rounded-square"
height="50" width="75"
style="border:1px"
asp-append-version="true" accept="image/*" />
<span><a href="@Model.Member.ImageName">@Model.Member.ImageName</a></span>
<div id="chooseFile">
<input class="form-control" type="file" name="photo" accept="image/*" />
</div>
</div>
</div>
</div>
这是我的 ViewModel 名称 MemberVieModel
using System;
using TeamManagement.Models;
namespace TeamManagement.ViewModel
{
public class MemberViewModel
{
public Member Member { get; set; }
public IFormFile? Photo { get; set; }
}
}
查看模型截图
那是我尝试了你的代码,当我在我的代码上实施时 运行 他们给出了那个错误
I get error when I click on edit button it show error in img src (null reference)
这是因为您没有按照说明进行操作。您没有在 form
的开头设置 <input type="hidden" asp-for="Member.MemberId" />
,因此编译器无法获取需要更新 Id
的 Id。因此,将 form
顶部的 Member.MemberId
设置为隐藏项,对于 'edit':
是强制性的
您将在以下位置遇到的错误:
var objMember = _context.Member.Where(mId => mId.MemberId == model.Member.MemberId).FirstOrDefault();
注意:当你不在你的form
设置<input type="hidden" asp-for="Member.MemberId" />
时,它不会得到需要更新的引用并抛出你得到的错误。
解法:
<form asp-action="EditMember" method="post" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly"></div>
<input type="hidden" asp-for="Member.MemberId" />
</form>
还有一点:
确保您已将 [Key]
设置在 Member
模型的顶部。它应该如下所示:
public class Member
{
[Key]
public int MemberId { get; set; }
[StringLength(60, MinimumLength = 3)]
public string? Name { get; set; }
public string? Gender { get; set; }
public DateTime DOB { get; set; }
public string? ImageName { get; set; }
public string? ImageLocation { get; set; }
}
当我点击编辑按钮时出现错误,它在 img src 中显示错误(空引用)
这是成员控制器的编辑功能
public async Task<IActionResult> Edit(int memberId)
{
var memeber = await _context.Member.FindAsync(memberId); // Getting member by Id from database
return View(new MemberViewModel() { Member = memeber });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(MemberViewModel model, IFormFile photo/*int id, [Bind("MemberId,Name,Gender,DOB,MaritalStatus,Address,PhoneNo,Skills,Hobbies,JobTitle,Technology")] Member member*/)
{
if (photo == null || photo.Length == 0)
{
return Content("File not selected");
}
var path = Path.Combine(_webHostEnviroment.WebRootPath, "ImageName/Cover", photo.FileName);
using (FileStream stream = new FileStream(path, FileMode.Create))
{
await photo.CopyToAsync(stream);
stream.Close();
}
model.Member.ImageName = photo.FileName;
//Finding the member by its Id which we would update
var objMember = _context.Member.Where(mId => mId.MemberId == model.Member.MemberId).FirstOrDefault();
if (objMember != null)
{
//Update the existing member with new value
objMember!.Name = model.Member.Name;
objMember!.Gender = model.Member.Gender;
objMember!.DOB = model.Member.DOB;
objMember!.MaritalStatus = model.Member.MaritalStatus;
objMember!.Address = model.Member.Address;
objMember!.PhoneNo = model.Member.PhoneNo;
objMember!.Skills = model.Member.Skills;
objMember!.Hobbies = model.Member.Hobbies;
objMember!.JobTitle = model.Member.JobTitle;
objMember!.Technology = model.Member.Technology;
objMember!.ImageName = model.Member.ImageName;
objMember!.ImageLocation = path;
await _context.SaveChangesAsync();
}
return RedirectToAction("Index");
}
这是一个出现错误的视图
@model TeamManagement.ViewModel.MemberViewModel
<form asp-action="Edit" method="post" enctype="multipart/form-data">
<div class="row">
<div class="form-group col-md-4">
<label class="col-md-2 control-label">MemberPicture</label>
<div class="col-md-10">
<img src="~/ImageName/Cover/@Model.Member.ImageName"
class="rounded-square"
height="50" width="75"
style="border:1px"
asp-append-version="true" accept="image/*" />
<span><a href="@Model.Member.ImageName">@Model.Member.ImageName</a></span>
<div id="chooseFile">
<input class="form-control" type="file" name="photo" accept="image/*" />
</div>
</div>
</div>
</div>
这是我的 ViewModel 名称 MemberVieModel
using System;
using TeamManagement.Models;
namespace TeamManagement.ViewModel
{
public class MemberViewModel
{
public Member Member { get; set; }
public IFormFile? Photo { get; set; }
}
}
查看模型截图
那是我尝试了你的代码,当我在我的代码上实施时 运行 他们给出了那个错误
I get error when I click on edit button it show error in img src (null reference)
这是因为您没有按照说明进行操作。您没有在 form
的开头设置 <input type="hidden" asp-for="Member.MemberId" />
,因此编译器无法获取需要更新 Id
的 Id。因此,将 form
顶部的 Member.MemberId
设置为隐藏项,对于 'edit':
您将在以下位置遇到的错误:
var objMember = _context.Member.Where(mId => mId.MemberId == model.Member.MemberId).FirstOrDefault();
注意:当你不在你的form
设置<input type="hidden" asp-for="Member.MemberId" />
时,它不会得到需要更新的引用并抛出你得到的错误。
解法:
<form asp-action="EditMember" method="post" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly"></div>
<input type="hidden" asp-for="Member.MemberId" />
</form>
还有一点:
确保您已将 [Key]
设置在 Member
模型的顶部。它应该如下所示:
public class Member
{
[Key]
public int MemberId { get; set; }
[StringLength(60, MinimumLength = 3)]
public string? Name { get; set; }
public string? Gender { get; set; }
public DateTime DOB { get; set; }
public string? ImageName { get; set; }
public string? ImageLocation { get; set; }
}