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...