Various options of passing data from ASP.NET MVC Controller to View

In ASP.NET MVC framework, we have got various options for passing data from Controller to View. Some of the options that i know and used are listed below :-

1. Use a strongly typed model object

2. Use a dynamic type (using @model syntax)

3.Use a ViewBag

This article focuses on explaining the above three methods of passing data from Controller to View.  Let’s create a simple ASP.NET MVC application to demo this one. Since there is a Cricket world cup fever going now, i will be creating a sample MVC application to display list of countries.

I’m adding the below model to the project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; 

namespace MVCDemo1.Models
{
    public class Countries
    { 

        public String Name { get; set; }
        public string Group { get; set; }
        public string DressColor { get; set; }
        public int NoOfTropies { get; set; }
    }
} 

I’ll be creating a new controller by name Country to handle various types of views(strongly typed views, loosely typed views and views accessing viewbag objects directly for the model Countries.

In the CountryController.cs, i’m setting up the values for CountryCollection

List<Countries> oCountries = new List<Countries> { 
        new Countries{Name="India",NoOfTropies=2,Group="A",DressColor="Blue"},
        new Countries{Name="Australia",NoOfTropies=3,Group="B",DressColor="Yellow"},
        new Countries{Name="SouthAfrica",NoOfTropies=0,Group="C",DressColor="Purple"}

        };

1.Use a strongly typed model object to pass data from Controller to View

In this approach, I’m adding a Controller action by name ‘StrongView’ to return the View for ActionResult.

public ActionResult StrongView()
        {

            return View(oCountries);

        }

Right click on the controller action and Add a View by choosing the Model class, which makes it a Strongly typed view.

image

Run the Solution and the View with CRUD operations look like below.

image

At the top of the View, model is referenced strongly which makes it Strongly typed View.

@model IEnumerable<MVCDemo1.Models.Countries>

Now, let’s move to the approach #2.

 

2. Use a dynamic type (@model syntax) to pass data from Controller to View

Let’s add another controller action by name ‘LooseView’ for passing loosely typed objects from Controller to View.

 public ActionResult LooseView()
        {

            return View(oCountries);
        
        }

Right click on the Controller action, Add View and do not choose any model class, that will make it as dynamically typed.

image

At the top of the View add a reference to dynamically typed view and Grab the Model data from dynamic object.

@model dynamic
@{
    ViewBag.Title = "LooseView";
}

<h2>LooseView</h2>

@foreach(var oCountry in Model)
{
    @oCountry.Name;
}

image

Now let’s move to the approach #3.

3. Use ViewBag to pass the data from Controller to View

Now let’s add a Controller action by name ‘ThirdView’ which sets the Country Collection and returns the View.

  public ActionResult ThirdView()
        {
            ViewBag.Countries = oCountries;
            return View();

        }

Let’s add a View by name ‘ThirdView’

image

I’ll be fetching the Model object from ViewBag and rendering it in the View.

@{
    ViewBag.Title = "ThirdView";
}

<h2>ThirdView</h2>

@foreach (var oCountry in ViewBag.Countries)
{
    @oCountry.Name;
}

image

 Subscribe to my blog

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.