嗨,有人可以解释一下 Visual Studio 生成的默认删除方法是如何工作的。我很困惑,因为当您发布DeleteConfirmed
模型时,模型Id
被传递回控制器。但是在视图中,Id
即使在隐藏字段中也没有生成字段,那么为什么Id
在发布时不会丢失/重置?控制器如何知道Id
?
看法
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>SellersQuery</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.Name)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Manufacturer)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Manufacturer)
</div>
</fieldset>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
控制器
public ActionResult Delete(int id = 0)
{
SellersQuery sellersquery = db.SellerQuerySet.Find(id);
if (sellersquery == null)
{
return HttpNotFound();
}
return View(sellersquery);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
SellersQuery sellersquery = db.SellerQuerySet.Find(id);
db.SellerQuerySet.Remove(sellersquery);
db.SaveChanges();
return RedirectToAction("Index");
}