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

MVC - Pass Data From Controller To View

Article:
Viewed:  1930 
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
    <%= ViewData["MyData"].ToString() %>
    </div>
</body>
</html> 


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

1.png

Image 1.

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