Lab 6 - Update and delete data from the table through ASP .NET Core
Lab 6 - Update and delete data from the table through ASP .NET Core
Net Core
Lab 3 – Managing data in SQL Server with ASP .Net Core: Part 2
Estimated usage time: 1 Hour
Lab 3.3. Update data in SQL Server through ASP .Net Core.
This tutorial will teach the student how to update data in the SQL database.
3. Copy the function name from the button asp-action = “” (as shown in the red highlighted below).
4. Now, go to Solution Explorer > go to Controllers Folder > Double clicks on the
FlowersController.cs File.
if(flower == null)
{
return BadRequest(FlowerID + " is not found in the table!");
}
return View(flower);
}
6. Now, highlight the EditData() function and right click on the highlighted function. Select
Add View and generate a new page for this function.
7. In the Add New Scaffolded Item dialog, select Razor View and press button Add.
8. In the Add Razor View dialog, directly create on the Add button.
10. Now, in the EditData.cshtml, add the below sentence to display the current existing data
in the page.
@model MVC_APU_FlowerShop2023.Models.Flower
<center>
<h1>Edit Current Flower Information:</h1>
<br /><hr /><br />
<form method="post" asp-action="UpdateData" asp-controller="Flowers">
<table border='1'>
<tr>
<th><label asp-for="FlowerID"></label></th>
<td><input asp-for="FlowerID" readonly /></td>
</tr>
<tr>
<th><label asp-for="FlowerName"></label></th>
<td><input asp-for="FlowerName"/></td>
<td><span asp-validation-for="FlowerName"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerType"></label></th>
<td><input asp-for="FlowerType" /></td>
<td><span asp-validation-for="FlowerType"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerPrice"></label></th>
<td><input asp-for="FlowerPrice" /></td>
<td><span asp-validation-for="FlowerPrice"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerProducedDate"></label></th>
<td><input asp-for="FlowerProducedDate" /></td>
<td><span asp-validation-for="FlowerProducedDate"></span></td>
</tr>
<tr>
<td colspan="3" style="text-align:center">
<button type="submit" class="btn-info">Update Flower
Information</button>
</td>
</tr>
</table>
</form>
</center>
11. Now, click on the Start Without Debugging button, you will see the below page appear
after you clicked on the Edit button in the Index.cshtml page.
12. To update the latest data in the Flower table, we need to write a function named
ProcessUpdateData in the controller file to process the update action.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UpdateData(Flower flower)
{
try
{
if (ModelState.IsValid)
{
_context.FlowerTable.Update(flower);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Flowers");
}
return View("EditData", flower);
}
catch(Exception ex)
{
return BadRequest("Error: "+ ex.Message);
}
}
13. To test the function, we can change the current flower information and see if it changes in
the database.
Lab 3.4. Delete Data from the tables of SQL Server with ASP .Net Core.
This tutorial will teach the student how to delete data from a table in the SQL database.
3. Now, click on the Start Without Debugging button and restart the website again.
4. To test the function, we can click on the Delete button and see if the selected flower
information is deleted from the database.
Summary:
In this tutorial, we have learnt how to edit data in database through the existing ASP .Net Core
project. Besides, in the next tutorial, we have also learnt how to delete record from the table
through the existing ASP.NET Core project.
In the next tutorial, we will learn how to migrate the tables from local database to Amazon
Relational Database Services through Visual Studio.