ASP.NET 8 MVC Tutorial for Beginners - C# web development made easy
Table of Contents
Introduction
This tutorial will guide you through building a web application using ASP.NET MVC with C#. You will learn how to create an expense tracking application called "Spend Smart" from scratch. This application will allow users to add, edit, delete, and view their expenses, utilizing an in-memory database with Entity Framework.
Chapter 1: Setting Up the ASP.NET MVC Project
-
Open Visual Studio 2022
- Create a new project by selecting
ASP.NET Core Web App (Model-View-Controller)
as the template. - Name the project "Spend Smart" and ensure you select
.NET 8
without any authentication or Docker options enabled.
- Create a new project by selecting
-
Run the Application
- Start the application by clicking the debug button or pressing
Ctrl + F5
. - Confirm that the application is running and accessible via Localhost at a specified port.
- Start the application by clicking the debug button or pressing
-
Understand the Project Structure
- Familiarize yourself with the folders:
Controllers
,Models
, andViews
. - The default controller is
HomeController
, which contains anIndex
method rendering the homepage.
- Familiarize yourself with the folders:
Chapter 2: Creating the Expense Model
-
Add a New Model
- Right-click the
Models
folder and selectAdd
>Class
. - Name the class
Expense
and define properties:public class Expense { public int ID { get; set; } public decimal Value { get; set; } public string? Description { get; set; } }
- Right-click the
-
Create a View for Displaying Expenses
- Right-click the
Views
folder, chooseAdd
>View
, and name itExpenses
. - Set up the view to display a list of expenses.
- Right-click the
-
Implement Routing Logic
- In
Program.cs
, configure the routing to include the new expenses view:app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
- In
Chapter 3: Adding Functionality to Create New Expenses
-
Create a Form for New Expenses
- Right-click on the
HomeController
, add a new action calledCreateEditExpense
. - Create a view for this action with a form allowing users to input ID, Value, and Description.
- Right-click on the
-
Submit the Form
- Update the form to submit to the
CreateEditExpense
action:<form asp-action="CreateEditExpense" method="post"> <!-- Form fields for ID, Value, Description --> <button type="submit" class="btn btn-primary">Submit</button> </form>
- Update the form to submit to the
-
Handle the Form Submission in the Controller
- Add logic to
CreateEditExpense
to save the expense to the database. Use Entity Framework to add the expense to the in-memory database.
- Add logic to
Chapter 4: Connecting to an In-Memory Database
-
Install Entity Framework Packages
- Use NuGet Package Manager to install:
Microsoft.EntityFrameworkCore.InMemory
Microsoft.EntityFrameworkCore
- Use NuGet Package Manager to install:
-
Set Up Database Context
- Create
SpendSmartDbContext
derived fromDbContext
and include aDbSet<Expense>
property.
- Create
-
Configure the Database Context in Program.cs
- Add the context to the service container:
builder.Services.AddDbContext<SpendSmartDbContext>(options => options.UseInMemoryDatabase("SpendSmartDB"));
- Add the context to the service container:
Chapter 5: Displaying and Managing Expenses
-
Display Expenses in the View
- Modify the expenses view to show a table of all expenses, including columns for ID, Value, Description, and Actions (Edit/Delete).
-
Implement Editing and Deleting Expenses
- Add actions in
HomeController
to handle editing and deleting expenses. - Update the expenses view to include links/buttons for editing and deleting:
<a asp-action="CreateEditExpense" asp-route-id="@expense.ID">Edit</a> <a asp-action="DeleteExpense" asp-route-id="@expense.ID">Delete</a>
- Add actions in
-
Handle Deletion Logic
- In the
DeleteExpense
action, find the expense by ID and remove it from the database, then redirect to the expenses view.
- In the
-
Add Total Expenses Calculation
- In the expenses view, calculate the total of all expenses and display it at the top of the list using a
ViewBag
:ViewBag.TotalExpenses = context.Expenses.Sum(e => e.Value);
- In the expenses view, calculate the total of all expenses and display it at the top of the list using a
Conclusion
You've successfully built a comprehensive expense tracking application using ASP.NET MVC and C#. You learned how to set up a project, create models, implement CRUD operations, and display data in a user-friendly interface. As a next step, consider exploring further features like user authentication, data persistence with SQL databases, or enhancing the UI with more advanced front-end frameworks.