How to Pass data from Controller to View in ASP.NET MVC Application

Hi Friend, Here i will show you how to pass data (information) from Controller to View in mvc framework .Here i will use only Controller and View components of the MVC template. We will learn each concepts of MVC one by one from simple level to difficult level. First we shall learn "How to create MVC template in Your visual studio".
  • If you are using visual studio 2008 with sp1 or any or free visual web developers 2008 express with sp1,It's not include MVC 2 by default.You can download ASP.NET MVC 2 template from and install it.
  • If you are using visual studio 2010 any's already include ASP.NET mvc 2 by default.   
  • If you are using Visual studio 2012  or Visual studio  2013 edition, then default template may be changed.but concepts is same for all MVC template (eg. MVC , MVC2 , MVC3 , MVC4.MVC5, MVC6 ) .
There are some steps to implement this concepts in your visual studio as given below:-

Step 1 :- First open your visual studio --> File--> New-->Project --> Web -->Select ASP.NET MVC2 Empty Web Application as shown below:-

Step 2 :- Now open solution Explorer --> You see the following components (Model, View , Controller) as shown below:- 

Step 3 :- Now Right click on Controller Folder in Solution Explorer window -->Add --> Controller --> Write Controller Name (MyfirstController) --> Press Add Button as shown below:-
controller class

Step 4 :- Now write the c# codes in controller class as given below:-

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

namespace FirstMVC_Application.Controllers
    public class MyfirstController : Controller
        // GET: /Myfirst/

        public ActionResult Index()
            ViewData["title"] = "Welcome to";
            ViewData["Date_Time"] = DateTime.Now.ToString();
            return View();


Step 5 :-Now Right click on ActionResult in coding part --> Add view as shown below:-

Step 6 :- Now open Index.aspx page --> write the codes as given below:-

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
   <b> <%= ViewData["title"] %></b>
    <br />
    The current Date and time in your System is: <%= ViewData["Date_Time"] %>

Note:- You can see,Index.aspx page in your View Folder in your solution Explorer Window.You can add Index.aspx page from here also after right click on Myfirst folder inside view folder. 
Step 7 :-Now Run the Application (Press F5) --> You will see following error as shown below:-

Note:-This error came because we didn't invoke any Actions.
Step 8 :- I have already created Myfirst Action but didn't invoke it.So type the URL as show below in Snapshot.
Note :- This Myfirst action invokes the Index.aspx page and display it on the browser screen.
For More...
  1. How to implement Page life cycle concepts in
  2. Introduction of Trigger in sql server
  3. How to implement WCF service in application
  4. How to create generic handlers in
  5. How to implement cookie concepts in
  6. How to implement validation control in
  7. How to implement exception handling concepts in c#
  8. String and string builder concepts in c#
Download whole Attached file


Post a Comment

Powered by Blogger.