Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

MVC - Pass Data From Controller To View

Viewed:  2432 
Posted On:  20/10/2013 06:20:41 
How we can pass data from controller to view in MVC ? 

In this article I am going to explain how we can pass data from controller to View. As we learn how to create first MVC application in my previous article.

MVC - Introduction and First Application in MVC

After adding New controller and their view go in controller PassDataController and in ActionResult store your data in ViewData like below . . .

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

namespace PassDataFromController2View.Controllers
    public class PassDataController : Controller
        // GET: /PassData/

        public ActionResult Index()
            ViewData["MyData"] = "Hi This data is coming from controller...";
            return View();


After this come to view. . . 

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> 

<html xmlns="" >
<head runat="server">
    <%= ViewData["MyData"].ToString() %>

 Now run your application by typing your controller name in url…


Image 1.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us