ASP.NET MVC - Passing Data From Controller To View

Introduction

There are many ways to pass Model class data to the View using a Controller class. In this article, I'm showing how to pass the data from Controller to View by using a simple example. Thus, let’s start and flip to your Visual Studio 2015.

Method 1 to pass data to View

New Project

Step 1. Give a name to your empty ASP.NET Web Application and click the OK button.

Project Name

I don’t want to use a full MVC-based solution. The article is based on a simple example, so just select an Empty template.

Select Template

Step 2. Add a Model class by right-clicking on the Models folder under Solution.

Add Class

Step 3. Select a class and give a name to your class.

Name CLass

Just type a few words of the code snippet for automatically implementing property and press the tab button available on the keyboard two times, which is a good practice, when the code is long.

Code Snippet

The public Record model class contains only three properties, which we want to pass to Views.

Public Record Model

public class Record  
{  
    public int Id { get; set; }  
    public string  RecordName { get; set; }  
    public string  RecordDetail { get; set; }  
         
}  

Step 4. Add a controller class by right-clicking on the Controllers folder.

Add Controller

Step 5. Select an Empty Controller from the list of Scaffold.

Select  Controller

Step 6. Give a meaningful name to your Controller.

Controller Name

The controller is ready now. By using ViewBag, we can pass the data from Controller to View.

Note. ViewBag gets the dynamic view data dictionary. 

Step 7. Create an object of your Model class and resolve it by namespace.

Create Object

I have created an object of the model class and assigned it to in ViewBag data dictionary. Assign the value for the property based on the model class. 

public ActionResult Index()  
{  
    Record rec = new Record  
    {  
        Id = 101,  
        RecordName = "Bouchers",  
        RecordDetail = "The basic stocks"  
    };  
    ViewBag.Message = rec;  
    return View();  
} 

  Step 8. Add a View for an Index Action by right-clicking on it.

Add A view

Step 9. Give a name to it and select the Add button.

View Name

First of all, import the model class.

Import Model

Assign viewbag into a variable and all the properties will be in place, using the variable and Razor block. 

@using PassDatainMVC.Models  
  
@{  
    ViewBag.Title = "Index";  
}  
  
<h3>Passing Data From Controller to View using ViewBag</h3>  
@{   
    var data = ViewBag.Message;  
}  
<h3>Id: @data.Id</h3>  
<h3>RecordName: @data.RecordName</h3>  
<h3>RecordDetail: @data.RecordDetail</h3>

  Step 10. Build and run your Application. You will get ViewBag Data.

Build Application

Method 2 to pass data to View

The other way of passing the data from Controller to View is ViewData. Also, a dictionary-type object is similar to ViewBag. There are no huge changes in Controller, and ViewData contains key-value pairs.

Method 2

public ActionResult Index()  
{  
    Record rec = new Record  
    {  
        Id = 101,  
        RecordName = "Bouchers",  
        RecordDetail = "The basic stocks"  
    };  
    ViewData["Message"] = rec;  
    return View();  
}  

Access your model class when you are using ViewData, as shown below.

Access Model

@using PassDatainMVC.Models  
@{  
    ViewBag.Title = "Index";  
}  
<h3>Passing Data From Controller To View using ViewData</h3>  
@{  
    var data = (Record)ViewData["Message"];  
}  
<h3>Id: @data.Id</h3>  
<h3>RecordName: @data.RecordName</h3>  
<h3>RecordDetail: @data.RecordDetail</h3>   

Save and Run your project. You will get the expected result from the ViewData.

Build Application Method 2

Method 3 to pass data to View

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of the model class in return view.

Method 3

public ActionResult Index()  
{  
    Record rec = new Record  
    {  
        Id = 101,  
        RecordName = "Bouchers",  
        RecordDetail = "The basic stocks"  
    };  
    return View(rec);  
}   

Import the binding object of the model class at the top of Index View and access the properties by @Model.

Import Object

@using PassDatainMVC.Models  
@model PassDatainMVC.Models.Record  
@{  
    ViewBag.Title = "Index";  
}  
<h3>Passing Data From Controller To View using Model Class Object</h3>  
  
<h3>Id: @Model.Id</h3>  
<h3>RecordName: @Model.RecordName</h3>  
<h3>RecordDetail: @Model.RecordDetail</h3>  

The expected result from the Model class Object is given.

Build Application method 3

Method 4 to pass data to View

The other way of passing the data is TempData, which is responsible for storing temporary data.

 After completing the subsequent request, the TempData will be cleared out. TempData is derived from TempDataDictinory. It is very useful to transfer non-sensitive data from one action method to another action method. As you can see in the screenshot given below, I have created an Action method for checking TempData. TempData transfers the data from the CheckTempData method to the Index method.

Method 4

public ActionResult CheckTempData()  
{  
    TempData["data"] = "I'm temprory data to used in subsequent request";  
    return RedirectToAction("Index");  
}   

Access TempData in Index.Chtml view is given.

<h3>Hey! @TempData["data"]</h3>

Run the Application and call the respective action method. TempData uses an internal session to store the data.

Build Application method 4

I hope you liked this article. Stay tuned with me for more on ASP.NET MVC, Web API and Microsoft Azure.


Similar Articles