Lab 3.1 - Create A Table, Add and View Data To or From The Table Through ASP .NET Core
Lab 3.1 - Create A Table, Add and View Data To or From The Table Through ASP .NET Core
Net Core
Lab 3 – Managing data in SQL Server with ASP .Net Core: Part 1
Estimated usage time: 1 Hour 50 Minutes
Lab 3.1. Create tables in SQL Server with ASP .Net Core.
This tutorial will teach the student how to create a table in the SQL database and how to add data
in the SQL table.
3. In the Add New Item dialog, named the class file as Flower.cs. Then, tap the Add button.
Flower ID Flower Name Flower Type Flower Produced Date Flower Price
Thus, edit the Flower.cs model class file based on the above table structure. Save the file after
finish editing the file.
using System.ComponentModel.DataAnnotations;
7. Then, add the new table details to the context class: MVC_APU_FlowerShop2023Context.cs.
using MVC_APU_FlowerShop2023.Models;
1. Now, start the PMC, click on the Tools > NuGet Package Manager > Package Manager
Console.
2. In the Visual Studio Package Manager Console, type the below commands:
Add-Migration addNewFlowerTable
c. Learn How to Manually Setup the Table in SQL Server Without Using Update-Database
Command.
(Estimation of Total Time Used: 15 minutes)
1. SQL Server Object Explorer (SSOX) in Visual Studio and create the table in the
MVC_APU_FlowerShop2023.
2. Right click the Databases > MVC_APU_FlowerShop2023 > Tables > Add New Table….
4. Now, click on the Update button to add the table to the database.
5. Finally, click on the Update Database button and the Flower table will be added to your current
database.
6. Next step we need to learn how to add the data to the Flower Table.
2. Go to Solution Explorer > Right click on the Controllers Folder > Select Add > Select
Controller.
3. In the Add New Scaffolded Item dialog, select MVC Controller – Empty. Then select
Add.
4. In the Add New Item dialog, select MVC Controller – Empty and name the file as
FlowersController.cs.
5. Then, select the Add button. A new controller has been added to your Controllers folder
now.
6. Now, highlight the Index() function and right click on the highlighted function. Select
Add View.
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 Index.cshtml, add the below sentence to link to your Index.cshtml page with
the coming Add Data page.
11. To add data to the Flower table, we need to create another .cshtml page. To create that
page, we must write a function named AddData in the controller file.
12. Now, same as the creation steps in Index.cshtml page, we must right click on the
AddData() function.
13. In the Add New Scaffolded Item dialog, select Razor View and press button Add.
14. In the Add Razor View dialog, directly create on the Add button.
16. Now, design the Add Data page using the below code:
@model MVC_APU_FlowerShop2023.Models.Flower
<center>
<h1>Add Data Example:</h1>
<hr />
<form asp-action="AddData" method="post">
<table>
<tr>
<th><label asp-for="FlowerName"></label> </th>
<td><input asp-for="FlowerName" required /></td>
<td><span asp-validation-for="FlowerName"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerType"></label> </th>
<td><input asp-for="FlowerType" required /></td>
<td><span asp-validation-for="FlowerType"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerProducedDate"></label> </th>
<td><input asp-for="FlowerProducedDate" required /></td>
<td><span asp-validation-for="FlowerProducedDate"></span></td>
</tr>
<tr>
<th><label asp-for="FlowerPrice"></label> </th>
<td><input asp-for="FlowerPrice" required /></td>
<td><span asp-validation-for="FlowerPrice"></span></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Add Data to table" />
</td>
</tr>
</table>
</form>
</center>
19. To connect to the database, add the below lines inside the controller.
private readonly MVC_APU_FlowerShop2023Context _context;
20. Now, add another AddData() function to receive the user inputs and store the inputs to the
database.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddData(Flower flower)
{
if (ModelState.IsValid)
{
_context.Add(flower);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(flower);
}
21. Now, to test the add data action, we must add the Flowers page link to the navigation bar.
Thus, we must open the _LoginPartial.cshtml and attach the Flowers > Index.cshtml
link inside the navigation bar.
<li class="nav-item">
<a id="addflower" class="nav-link text-dark" asp-action="Index" asp-controller="Flowers">
View Flower List</a>
</li>
22. Now, once every step for creating and adding the data is done, we can start testing our
application now.
23. Once added the data, let’s look at the database table. If you successfully add the data, the
data will appear in the Flower table now.
Lab 3.2. View Data from the tables of SQL Server with ASP .Net Core.
This tutorial will teach the student how to view data from a table in the SQL database.
3. Now, go back to the Index() function by right clicking on the Index() function.
4. Modify the Index.cshtml file with the below code so that it can display the current data
from the Flower table.
@model List<Flower>
<center>
<table border="1">
<tr style="background-color: yellow;">
<th>Flower ID</th>
<th>Flower Name</th>
<th>Flower Type</th>
<th>Flower Produced Date</th>
<th>Flower Price</th>
<th>Update Action</th>
<th>Delete Action</th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>@item.FlowerID</td>
<td>@item.FlowerName</td>
<td>@item.FlowerType</td>
<td>@item.FlowerProducedDate</td>
<td>@item.FlowerPrice</td>
<form asp-action="DeleteData" asp-controller="Flowers" asp-route-
FlowerId="@item.FlowerID">
<td><button>Delete</button></td>
<td><button asp-action="EditData" asp-controller="Flowers" asp-route-
FlowerId="@item.FlowerID" >Edit</button></td>
</form>
</tr>
}
</table>
</center>
5. Now, click on the Start Without Debugging button and restart the website again. You
should see the data displayed in the current Index.cshtml in the Flower Folder.
Summary:
In this tutorial, we have learnt how to create a new table and add data into the new table through
the existing ASP .Net Core project. Besides, in the next tutorial, we have also learnt how to read
records from the table through the existing ASP.NET Core project.
In the next tutorial, we will learn how to update and delete records from / to the table using the
existing ASP .NET Core project.