In Module 9, we move from the “behind-the-scenes” logic of APIs into building Full-Stack Web Applications. ASP.NET Core MVC (Model-View-Controller) is a design pattern that separates an application into three main components to make it easier to manage and scale.
The goal of MVC is Separation of Concerns. Each part of the pattern has a specific job:
Product class or a DatabaseContext).Razor is a markup syntax that lets you embed C# code into HTML. Files use the .cshtml extension.
@ symbol to transition from HTML to C#.<ul> @foreach (var item in Model.Products) { <li>@item.Name - @item.Price.ToString("C")</li> } </ul><a asp-action="Details" asp-route-id="@item.Id">Details</a>).To avoid repeating HTML (like headers and footers) on every page, we use a modular system:
_Layout.cshtml): The “Master Page.” It contains the <html>, <head>, and <body> tags. Every view is injected into the @RenderBody() placeholder._LoginPartial.cshtml) that can be embedded into multiple pages.In a professional app, you never pass your database entity directly to a View.
User might have a PasswordHash property. You don’t want that sent to the browser.ProductDashboardViewModel that contains both.MVC makes it incredibly easy to take user input and turn it into data.
[Required]), MVC can automatically display error messages next to input fields using <span asp-validation-for="Name"></span>.Sometimes you need to pass small bits of info that aren’t part of your formal “Model.”
| Method | Lifetime | Usage |
| ViewData | Current Request | Dictionary-based; requires type casting. |
| ViewBag | Current Request | Dynamic wrapper around ViewData; no casting needed. |
| TempData | Until Read | Uses Sessions; perfect for “Success” messages after a redirect. |
/Product/Index.ProductController and the Index method.View(productList)..cshtml file, replaces C# code with HTML.