问题描述: 在ASP.NET Core项目中,使用MVC和Entity Framework时,编辑方法不显示值。
解决方法:
asp-for
属性指定模型属性。
或者使用HtmlHelper
的TextBoxFor
方法。
@Html.TextBoxFor(model => model.PropertyName, new { @class = "form-control" })
public IActionResult Edit(int id)
{
var entity = dbContext.EntityName.Find(id);
return View(entity);
}
确保EntityName
是你的实体类的名称。
或者使用HtmlHelper
的TextBoxFor
方法。
@Html.TextBoxFor(model => model.PropertyName, new { @class = "form-control", @Value = Model.PropertyName })
[HttpPost]
public IActionResult Edit(EntityName entity)
{
if (ModelState.IsValid)
{
dbContext.Update(entity);
dbContext.SaveChanges();
return RedirectToAction("Index");
}
return View(entity);
}
确保EntityName
是你的实体类的名称。
通过上述步骤,你应该能够解决ASP.NET Core中使用MVC和Entity Framework时编辑方法不显示值的问题。