在 ASP.NET Core 和 Entity Framework Core 的同一视图中使用不同的 CRUD 提交按钮
Use Different CRUD Submit Button in Same View in ASP.NET Core with Entity Framework Core
我通过 Entity Framework Core 通过 ASP.NET Core 上的 Razor 页面结构中的三个不同按钮管理数据库的读取、更新和删除过程。
我正在通过 ASP.NET 核心控制器向 .cshtml 页面发送 class;此 class 包含一个数据库列表和一个数据库列表项。这样,如果数据库列表项下有一行,我就可以显示整个数据库列表。
我在这个 .cshtml 中放置的这三个不同按钮中的 "delete" 按钮没有任何问题,因为我可以通过此按钮发送 getID 从数据库中删除所选数据。
同样,当我在这个.cshtml中使用"delete"和"read"或"delete"和"update"按钮时,我仍然没有问题。
我的问题是,当"read"、"update"和"delete"同时出现时,我无法确定按钮的不同触发分别进入同一个[HttpPost]视图连接到控制器。
我想做的很简单:
如果按下 "read" 按钮,属于 "id" 值的项目应根据输入的数据库 "id" 值在 .cshtml 中显示在单行中。
如果按下 "update" 按钮,则应根据输入的数据库 "id" 值在 .cshtml 中的单行项目中进行更改。
如果按下"delete"按钮,必须删除输入数据库"id"值的数据库项。
所有这些按钮都在一个 .cshtml 中,并且所有 return 都在一个 "post" 值中,但是在控制器上有三个不同的选项我无法理解按下了哪个按钮。
注意:我留在这里的代码完美的Update a row in DB and Delete a row in DB but couldn't be reached Read a row in DB.当读取方法时,同样更改数据和路由到 UpdateDB。因为,我已经成功删除了数据库中的一行 returning id 值;但是 Read 和 Update 需要不同的 HttpPost 结构,所以我认为
示例代码结构:
在ASP.NET核心View.cshtml和HomeController.cs
中发送C#Class
//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>
My problem is that when "read", "update" and "delete" are simultaneous, I cannot determine the different triggering from the buttons separately into the same [HttpPost] View connected to the Controller.
您可以像 asp-route-myMethod
一样使用自定义 asp-route-{value}
并将其设置为 READ
或 UPDATE
作为标志,例如
<input type="submit" asp-route-myMethod="READ" 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" />
然后在POST
动作中,你可以从查询字符串中得到myMethod
参数来区分
[HttpPost]
public IActionResult Index(SendingClass item, [FromQuery] string myMethod = null)
{
item.OneRow.ID = item.GetID;
if (myMethod == "READ")
{
//For Read
}
else
{
//For Update
repository.UpdateDB(item.OneRow);
}
var send = new SendingClass
{
GetDB = repository.MyDB,
GetID = item.GetID,
OneRow = repository.GetByID(item.GetID)
};
ModelState.Clear();
return View(send);
}
我通过 Entity Framework Core 通过 ASP.NET Core 上的 Razor 页面结构中的三个不同按钮管理数据库的读取、更新和删除过程。
我正在通过 ASP.NET 核心控制器向 .cshtml 页面发送 class;此 class 包含一个数据库列表和一个数据库列表项。这样,如果数据库列表项下有一行,我就可以显示整个数据库列表。
我在这个 .cshtml 中放置的这三个不同按钮中的 "delete" 按钮没有任何问题,因为我可以通过此按钮发送 getID 从数据库中删除所选数据。 同样,当我在这个.cshtml中使用"delete"和"read"或"delete"和"update"按钮时,我仍然没有问题。
我的问题是,当"read"、"update"和"delete"同时出现时,我无法确定按钮的不同触发分别进入同一个[HttpPost]视图连接到控制器。
我想做的很简单:
如果按下 "read" 按钮,属于 "id" 值的项目应根据输入的数据库 "id" 值在 .cshtml 中显示在单行中。
如果按下 "update" 按钮,则应根据输入的数据库 "id" 值在 .cshtml 中的单行项目中进行更改。
如果按下"delete"按钮,必须删除输入数据库"id"值的数据库项。
所有这些按钮都在一个 .cshtml 中,并且所有 return 都在一个 "post" 值中,但是在控制器上有三个不同的选项我无法理解按下了哪个按钮。
注意:我留在这里的代码完美的Update a row in DB and Delete a row in DB but couldn't be reached Read a row in DB.当读取方法时,同样更改数据和路由到 UpdateDB。因为,我已经成功删除了数据库中的一行 returning id 值;但是 Read 和 Update 需要不同的 HttpPost 结构,所以我认为
示例代码结构:
在ASP.NET核心View.cshtml和HomeController.cs
中发送C#Class//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>
My problem is that when "read", "update" and "delete" are simultaneous, I cannot determine the different triggering from the buttons separately into the same [HttpPost] View connected to the Controller.
您可以像 asp-route-myMethod
一样使用自定义 asp-route-{value}
并将其设置为 READ
或 UPDATE
作为标志,例如
<input type="submit" asp-route-myMethod="READ" 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" />
然后在POST
动作中,你可以从查询字符串中得到myMethod
参数来区分
[HttpPost]
public IActionResult Index(SendingClass item, [FromQuery] string myMethod = null)
{
item.OneRow.ID = item.GetID;
if (myMethod == "READ")
{
//For Read
}
else
{
//For Update
repository.UpdateDB(item.OneRow);
}
var send = new SendingClass
{
GetDB = repository.MyDB,
GetID = item.GetID,
OneRow = repository.GetByID(item.GetID)
};
ModelState.Clear();
return View(send);
}