我正在使用 ASP.NET MVC 制作一個 webbapplication,并且我正在嘗試編輯我的物件串列。例如,如果我將產品添加到站點,然后單擊該產品的編輯以更改獎品,我只會獲得帶有新獎品的新物件,而不是將獎品更改為產品。
所以問題是,它沒有更新產品,而是添加了一個新產品。
這就是我的產品控制器的樣子:
using auktioner_MarcusR91.Data;
using auktioner_MarcusR91.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace auktioner_MarcusR91.Controllers
{
public class InventoryController : Controller
{
private readonly AppDbContext _db;
public InventoryController(AppDbContext db)
{
_db = db;
}
public IActionResult Index()
{
IEnumerable<Inventory> objInventoryList = _db.Inventories;
return View(objInventoryList);
}
//GET
public IActionResult Create()
{
return View();
}
//Post
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Inventory inventory)
{
_db.Inventories.Add(inventory);
_db.SaveChanges();
return RedirectToAction("index");
}
//GET
public IActionResult Edit(int? id)
{
if (id == 0 || id == 5)
{
return NotFound();
}
var inventoryFromDb = _db.Inventories.Find(id);
if (inventoryFromDb == null)
{
return NotFound();
}
return View(inventoryFromDb);
}
//Post
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(Inventory inventory)
{
if (ModelState.IsValid)
{
_db.Inventories.Update(inventory);
_db.SaveChanges();
return RedirectToAction("index");
}
return View(inventory);
}
}
}
我認為我的控制器有問題。
但是,當我編輯產品時,這也是我的看法:
@model Inventory
<form method = "post" asp-action = "Edit">
<div class = "border p-3 mt-4">
<div class = "row pb-2">
<h2 class = "text-primary">Edit Inventory</h2>
<hr />
</div>
<div class = "mb-3">
<label asp-for ="inventoryName"></label>
<input asp-for = "inventoryName" />
<label asp-for ="finalPrize"></label>
<input asp-for = "finalPrize" />
<label asp-for ="inventoryDesc"></label>
<input asp-for = "inventoryDesc" />
<p>1 f?r "Transport</p>
<p>2 f?r "Smycken"</p>
<p>3 f?r "Hush?ll"</p>
<p>4 f?r "Dekoration"</p>
<select asp-for = "categoryId">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
<button type = "submit" class = "btn btn-primary" width = "100px">Update</button>
<a asp-controller = "Inventory" asp-action = "index" class = "btn btn-secondary" style = "width: 100px">Back to products</a>
</div>
</form>
uj5u.com熱心網友回復:
您必須添加一個主鍵inventoryId 作為隱藏輸入,如果沒有此鍵,您的庫存實體看起來就像EF 的一個新實體。并且由于您在操作中使用 [ValidateAntiForgeryToken],因此請使用另一種表單語法將其添加到視圖中
@using (Html.BeginForm("Edit", "Inventory", FormMethod.Post))
{
@Html.AntiForgeryToken()
<input type="hidden" asp-for="inventoryId" value="@Model.inventoryId" />
....
<button type = "submit" class = "btn btn-primary" width = "100px">Update</button>
<a asp-controller = "Inventory" asp-action = "index" class = "btn btn-secondary" style = "width: 100px">Back to products</a>
</div>
}
恕我直言,您的更新代碼可能是這樣的
if (ModelState.IsValid)
{
var inventoryFromDb = _db.Inventories.Find(inventory.inventoryId);
if (inventoryFromDb == null)
{
return NotFound();
}
_db.Entry(inventoryFromDb).CurrentValues.SetValues(inventory);
var result= _db.SaveChanges();
}
uj5u.com熱心網友回復:
您必須通過單擊記錄的更新按鈕將記錄 ID 發送到控制器。像這樣的東西:
<a class="btn btn-warning btn-sm btn-margin" asp-controller="ContextController" asp-action="UpdateAction" ***asp-route-id="@item.Id***">Update</a>
其中@item 是模型發送到視圖的物件。行動將是:
[HttpGet]
public IActionResult UpdateAction(int id)
{
Model record = _Context.GetById(id);
return View("UpdateFormPageOrModal",record);
}
在更新表單并單擊視圖資料的提交按鈕后,將發送到操作:
[HttpPost]
public IActionResult UpdateAction(Model record)
{
var result = _Context.UpdateBy(record);
ViewData["Result"] = result.Message;
if (result.IsSucceeded)
{
_UnitOfWork.Save();
return RedirectToAction("TheGridView");
}
return View("UpdateView",record);
}
其中 UpdateBy() 方法應該是這樣的:
public void UpdateBy(T entity)//entity is an object of the DbSet<Model>
{
var state = _Context.Entry(entity).State;
if (state == EntityState.Detached)
{
_Context.Attach(entity);
}
_Context.Entry(entity).State = EntityState.Modified;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407658.html
標籤:
上一篇:在mvc的表中添加動態<tr>
