【发布时间】:2020-04-13 19:50:34
【问题描述】:
我通过 ASP.NET Core 上 Razor 页面结构中的三个不同按钮,通过 Entity Framework Core 从数据库中管理读取、更新和删除过程。
我正在通过 ASP.NET Core 控制器向 .cshtml 页面发送一个类;此类包含一个数据库列表和一个数据库列表项。这样,如果数据库列表项下有一行,我可以显示整个数据库列表。
我在此 .cshtml 中放置的这三个不同按钮中的“删除”按钮没有任何问题,因为我可以通过此按钮发送 getID 从数据库中删除所选数据。 同样,当我在这个.cshtml中使用“删除”和“读取”或“删除”和“更新”按钮时,我仍然没有问题。
我的问题是,当“读取”、“更新”和“删除”同时进行时,我无法确定从按钮分别触发到连接到控制器的同一个 [HttpPost] 视图中的不同触发。
我想做的很简单:
如果按下“读取”按钮,属于“id”值的项目应根据输入的数据库“id”值显示在.cshtml中的单行中。
如果按下“更新”按钮,则应根据输入的数据库“id”值对.cshtml中的单行项进行更改。
如果按下“删除”按钮,则必须删除输入数据库“id”值的数据库项。
所有这些按钮都在一个 .cshtml 中,并且都返回一个“post”值,但是在控制器上我无法理解按下哪个按钮的三个不同选项。
注意:我在这里留下的代码完美地更新了数据库中的一行并删除了数据库中的一行,但无法实现读取数据库中的一行。当 Read 方法时,同样改变数据和路由到 UpdateDB。因为,我已经成功删除 DB 中的一行,只返回 id 值;但是读取和更新需要不同的 HttpPost 结构,所以我认为
示例代码结构:
在 ASP.NET Core View.cshtml 和 HomeController.cs 中发送 C# 类
//Here is my sending class. "SendingClass" will be sent to Index.cshtml via HomeController
public class SendingClass
{
public IEnumerable<ProductDB> GetDB { get; set; }
public int GetID { get; set; }
public ProductDB OneRow { get; set; }
}
//Here is my HomeController
public class HomeController : Controller
{
private IProductRepository repository;
public HomeController(IProductRepository repo)
{
repository = repo;
}
public IActionResult DataDel(int id)
{
repository.DeleteDB(id);
var send = new SendingClass
{
GetDB = repository.MyDB,
GetID = repository.MyDB.First().ID,
OneRow = repository.GetByID(repository.MyDB.First().ID)
};
return RedirectToAction("Index");
}
[HttpGet]
public IActionResult Index()
{
var send = new SendingClass
{
GetDB = repository.MyDB,
GetID = repository.MyDB.First().ID,
OneRow = repository.GetByID(repository.MyDB.First().ID)
};
return View(send);
}
[HttpPost]
public IActionResult Index(SendingClass item)
{
item.OneRow.ID = item.GetID;
if (item.OneRow == null)
{
}
else
{
repository.UpdateDB(item.OneRow);
}
var send = new SendingClass
{
GetDB = repository.MyDB,
GetID = item.GetID,
OneRow = repository.GetByID(item.GetID)
};
return View(send);
}
}
//UpdateDB: My Ef.Core Database Update Method
//DeleteDB: My Ef.Core Database Delete Method
// repository.MyDB: My database which called in Ef.Core
这里是 View.cshtml
@model SendingClass
@{
}
<!DOCTYPE html>
<html>
<head>
</head>
<body class="align-items-center">
<form asp-action="Index" asp-controller="Home" method="post">
<label>Data Row Number</label>
<p></p>
<input type="text" asp-for="@Model.GetID" id="@Model.GetID" />
<p></p>
<input type="submit" value="READ" />
<p></p>
<input type="submit" value="UPDATE" />
<p></p>
<input type="submit" asp-action="DataDel" asp-route-id="@Model.GetID" id="@Model.GetID" value="DELETE" />
<p></p>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Customer Number</th>
<th>Price (Old)</th>
<th>Price (New)</th>
<th>Checkout</th>
</tr>
</thead>
<tbody>
<tr>
<td asp-for="@Model.OneRow.ID">@Html.DisplayFor(m => Model.OneRow.ID)</td>
<td asp-for="@Model.OneRow.Name">@Html.TextBoxFor(m => Model.OneRow.Name)</td>
<td asp-for="@Model.OneRow.CustomerNo">@Html.TextBoxFor(m => Model.OneRow.CustomerNo)</td>
<td asp-for="@Model.OneRow.PriceOld">@Html.TextBoxFor(m => Model.OneRow.PriceOld)</td>
<td asp-for="@Model.OneRow.PriceNew">@Html.TextBoxFor(m => Model.OneRow.PriceNew)</td>
<td asp-for="@Model.OneRow.Checkout">@Html.TextBoxFor(m => Model.OneRow.Checkout)</td>
</tr>
</tbody>
</table>
</form>
<p></p>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>NO</th>
<th>Price (Old)</th>
<th>Price (New)</th>
<th>Checkout</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.GetDB)
{
<tr>
<td>@Html.DisplayFor(m => item.ID)</td>
<td>@Html.DisplayFor(m => item.Name)</td>
<td>@Html.DisplayFor(m => item.CustomerNo)</td>
<td>@Html.DisplayFor(m => item.PriceOld)</td>
<td>@Html.DisplayFor(m => item.PriceNew)</td>
<td>@Html.DisplayFor(m => item.Checkout)</td>
</tr>
}
</tbody>
</table>
<p></p>
</body>
</html>
【问题讨论】:
标签: entity-framework asp.net-core http-post crud razor-pages