我的更新方法在Dbset不工作,它只是在数据库中添加另一行



我试图学习web开发与asp.net 6,我试图更新我创建的一个类别。我写了一个get和post方法,但它只是不工作在编辑动作,它是在数据库中添加一个新的行,而不是更新。

这是我在类别控制器

中的编辑动作
//GET
public IActionResult Edit(int? id)
{
if(id == null || id == 0)
{
return NotFound();
}
var CategoryFromDb = _db.Categories.Find(id);
if (CategoryFromDb == null)
{
return NotFound();
}
return View(CategoryFromDb);
}
//POST
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(Category obj)
{
if (obj.Name == obj.DisplayOrder.ToString())
{
ModelState.AddModelError("name", "The DisplayOrder cannot exactly match the Name.");
}
if (ModelState.IsValid)
{
_db.Categories.Update(obj);
_db.SaveChanges();
return RedirectToAction("Index", "Category");
}
return View(obj);
}
}

这是我的Edit.cshtml

@model Category
<form method="post" asp-action="Edit">
<div class="border p-3 mt-4">
<div class="row pb-2">
<h2 class="text-primary">Edit Category</h2>
<hr/>
<div asp-validation-summary="All"></div>
</div>
<div class="mb-3" style="  width: 500px;clear: both;">
<label asp-for="Name"></label>
<input style="width: 100%;clear: both;" asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-bg-danger"></span>
</div>
<div class="mb-3" style="  width: 500px;clear: both;">
<label asp-for="DisplayOrder"></label>
<input style="width: 100%;clear: both;" asp-for="DisplayOrder" class="form-control" />
<span asp-validation-for="DisplayOrder" class="text-bg-danger"></span>
</div>
<button type="submit" class="btn btn-primary" style="width:150px;">Update</button>
<a asp-controller="Category" asp-action="Index" class="btn btn-secondary" style="width:150px">Back to List</a>
</div>
</form>

这是我的分类模型

public class Category
{
[Key]
public int? CategoryID { get; set; }
[Required]
public string Name { get; set; }
[DisplayName("Display Order")]
[Range (1,100,ErrorMessage ="Not in Range of Display Order")]
public int DisplayOrder { get; set; }
public DateTime CreatedDateTime { get; set; } = DateTime.Now;
}

这是我创建的索引视图编辑按钮

<div class="w-75 btn-group" role="group">
<a asp-controller="Category" asp-action="Edit" asp-route-id ="@obj.CategoryID" ><i class="bi bi-pencil"></i>Edit</a>
</div>

,它向数据库添加新行,而不是基于主键更新数据库请帮帮我……由于

你的主要问题是你没有正确填写模型-也就是说,你的视图没有填写CategoryID,所以控制器不"知道";要更新的类别。你需要像这样修改你的视图(我在<form>下面添加了一行):

@model Category
<form method="post" asp-action="Edit">
@Html.HiddenFor(m => m.CategoryID)
<div class="border p-3 mt-4">
<div class="row pb-2">
<h2 class="text-primary">Edit Category</h2>
<hr/>
<div asp-validation-summary="All"></div>
</div>
<div class="mb-3" style="  width: 500px;clear: both;">
<label asp-for="Name"></label>
<input style="width: 100%;clear: both;" asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-bg-danger"></span>
</div>
<div class="mb-3" style="  width: 500px;clear: both;">
<label asp-for="DisplayOrder"></label>
<input style="width: 100%;clear: both;" asp-for="DisplayOrder" class="form-control" />
<span asp-validation-for="DisplayOrder" class="text-bg-danger"></span>
</div>
<button type="submit" class="btn btn-primary" style="width:150px;">Update</button>
<a asp-controller="Category" asp-action="Index" class="btn btn-secondary" style="width:150px">Back to List</a>
</div>
</form>

最新更新