notes for my setudents
ASP.NET
There are two approaches to building a Web App with ASP.NET:
1. Server-Side Rendered UI: this approach also offers two methods:
1.1. Razor Pages
1.2. MVC (Models, Views, and Controllers): in MVC, the Model contains data that is represented by the View. Put simply, the View displays the Model (Data) and handles user interactions. The Controller manages both the presentation (View) and the Model.
For instance, when you enter a website address in your browser, your request goes to a controller, like the home controller. This controller may fetch data from a database to create a model. The model represents the information the user wants, which could be a mix of data from different database tables. Next, the model is passed to a view. The view is what the user interacts with (the user interface), using the data to generate the response shown to the user. Also, if the user clicks a button on the view, it triggers another request to a different controller, restarting the cycle.
2. Client-Side Rendered UI: there are two methods here as well:
2.1. Blazor using C#, HTML, and CSS.
2.2. SPA (Single Page Application) using JavaScript Frameworks like Angular or React.
How to add a model that my view can use to display data:
- right-click on 📁Models > Add > Class…
- in the model file:
public class PersonViewModel
{
public string Name { get; set; }
public int Age { get; set; }
}
3. in the controller class:
public IActionResult Index()
{
PersonViewModel obj1 = new PersonViewModel() // create an object from the model
{
Name = "Ehsan",
Age = 98
};
return View(obj1); // pass the object to the view
}
4. in the Razor page:
@model MVCehsana.Models.PersonViewModel // the type of the model that the view expects would be PersonViewModel
<h1>Hello!</h1>
<h4>My name is @Model.Name, and I am @Model.Age years old.</h4>