Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ASP.NET Core 2 Fundamentals

You're reading from  ASP.NET Core 2 Fundamentals

Product type Book
Published in Aug 2018
Publisher
ISBN-13 9781789538915
Pages 298 pages
Edition 1st Edition
Languages
Authors (2):
Onur Gumus Onur Gumus
Author Profile Icon Onur Gumus
Onur Gumus
Mugilan T. S. Ragupathi Mugilan T. S. Ragupathi
Author Profile Icon Mugilan T. S. Ragupathi
Mugilan T. S. Ragupathi
View More author details
Toc

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:

Note

Go to https://goo.gl/oYH7am 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);
}

Make...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime