In this article will perform a simple demonstration of how we can pass data from controller to view. In this demo will perform this activity using Viewdata, which is a dictionary, derived from view data dictionary class. Viewdata basically holds the data which will be passed from controller to view.
Let's start our demo
Let's start our demo
Let's create a controller

Create a demo class PersonDetails, consisting of different parameters of details like name, age, email and city.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace ProjectMVC.Models
- {
- public class Employee
- {
- public string _name { get; set; }
- public int _age { get; set; }
- public string _email { get; set; }
- public string _city { get; set; }
- }
- }
After adding a model class,here is how we can get that model value in controller after creating object of that model class in the action of the controller. Make sure there is Namespace for recently added model so that we can get the values of recently added model class in controller.
- using ProjectMVC.Models;
Add following piece of code in controller's action, where we create the object of the class, which will get all the values(attributes) of the class into controller. We got values from model to controller.
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.Mvc;
- using ProjectMVC.Models;
- namespace ProjectMVC.Controllers
- {
- public class PersonController : Controller
- {
- public ActionResult GetEmployeeDetails()
- {
- Employee empdetails = new Employee();
- empdetails._name = "Shridhar Sharma";
- empdetails._age = 25;
- empdetails._email = "[email protected]";
- empdetails._city = "New Delhi";
- ViewData["Employeedetails"] = empdetails;
- return View("employeeview");
- }
- }
- }



Dinesh GabhanePosted Nov 11, 2019, 1:22 AM
Good article.
Shridhar SharmaPosted Oct 3, 2015, 11:29 PM
Thanks all for reading.
Sumit JoshiPosted Oct 3, 2015, 4:36 PM
thanks for sharing.
RakeshPosted Oct 3, 2015, 10:26 AM
Good article share
Akash VarshneyPosted Oct 3, 2015, 6:39 AM
Good Article!!
Abhay ShankerPosted Oct 3, 2015, 6:32 AM
Nice Post
Harshad PansuriyaPosted Oct 3, 2015, 4:58 AM
Nice One
Santhakumar MunuswamyPosted Oct 2, 2015, 9:47 AM
Good one
Nilesh JadavPosted Oct 2, 2015, 9:40 AM
Great post sir