Use Different CRUD Submit Button in Same View in ASP.NET Core with Entity Framework Core

  • Thread starter Thread starter Yusuf Ahmet Uzundeveli
  • Start date Start date
Y

Yusuf Ahmet Uzundeveli

Guest
Hello.
I manage the read, update and delete process from a database through Entity Framework Core via three different buttons in a Razor page structure on ASP.NET Core.

I'm sending a class to a .cshtml page via the ASP.NET Core controller; this class contains a database list and a database list item. This way I can show the entire database list if one row is under a database list item.

I don't have any problems with the "delete" button from these three different buttons that I placed in this .cshtml because I can delete the selected data from the database by sending a getID via this button.
Similarly, when I use the "delete" and "read" or "delete" and "update" buttons in this .cshtml, I still have no problems.

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.

What I want to do is very simple:

If the "read" button is pressed, the item belonging to the "id" value should be shown in a single line in .cshtml according to the entered database "id" value.

If the "update" button is pressed, changes should be made in the single line item in .cshtml according to the entered database "id" value.

If the "delete" button is pressed, the database item of the entered database "id" value must be deleted.

All of these buttons are in a single .cshtml and all return a single "post" value, but there are three different options that I cannot understand on the Controller which button is pressed.

Sample Code Structure:


//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

My 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>

Continue reading...
 
Back
Top