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…

Image 1.