Book Image

ASP.NET Core 2 Fundamentals

By : Onur Gumus, Mugilan T. S. Ragupathi
Book Image

ASP.NET Core 2 Fundamentals

By: Onur Gumus, Mugilan T. S. Ragupathi

Overview of this book

The book sets the stage with an introduction to web applications and helps you build an understanding of the tried-and-true MVC architecture. You learn all about views, from what is the Razor view engine to tagging helpers. You gain insight into what models are, how to bind them, and how to migrate database using the correct model. As you get comfortable with the world of ASP.NET, you learn about validation and routing. You also learn the advanced concepts, such as designing Rest Buy (a RESTful shopping cart application), creating entities for it, and creating EF context and migrations. By the time you are done reading the book, you will be able to optimally use ASP.NET to develop, unit test, and deploy applications like a pro.
Table of Contents (14 chapters)

Adding Models


Models represent your business domain classes. Now, we are going to learn about how to use the Models in our controller. Create a Models folder and add a simple Employee class. This is a just a plain old C# class:

Note

Go to https://goo.gl/uBtpw3 to access the code.

public class Employee
{
  public int EmployeeId { get; set; }
  public string Name { get; set; }
  public string Designation { get; set; }
}

Create a new action method, Employee, in our HomeController, and create an object of the Employee Model with some values, and pass the Model to the View. Our idea is to use the Model employee values in the View to present them to the user:

Note

Go to https://goo.gl/r4Jc9x to access the code.

public IActionResult Employee()
{
  //Sample Model - Usually this comes from database
  Employee emp1 = new Employee
  {
    EmployeeId = 1,
    Name = "Jon Skeet",
    Designation = " Software Architect"
  };
  return View(emp1);
}

Now, we need to add the respective View for this action method...