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)

Passing Data from the Controller to the View

We have just discussed how to pass the data from the controller to the view using the Model object. While calling the view, we are passing the model data as a parameter. But there are times when you want to pass some temporary data to the view from the controller. This temporary data may not deserve a model class. In such scenarios, we can use either ViewBag or ViewData.

ViewData is the dictionary and ViewBag is the dynamic representation of the same value.

Let us add the company name and company location property using ViewBag and ViewData, as shown in the following code snippet:


Go to 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"
  ViewBag.Company = "Google Inc";
  ViewData["CompanyLocation"] = "United States";
  return View(emp1);