Passing data from Controller to View

Viewdata/Viewbag/Tempdata is used to pass data from CONTROLLER to VIEW.
By seeing the picture,you can understand how data is passed from controller to view.
Passing Data from Controller to View:-
Let us do it Step by Step:-
Step 1: Create a new project named "ControllerToView" in Visual studio 2015

Step 2: Select MVC template

Step 3:You will see the following image.

Step 4:Now Add "MySiteController" to controller folder


Step 5:Go to Route.config file in App_Start folder
Step 6:Change the default route to "MySite" as below

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MySite", action = "Index", id = UrlParameter.Optional }
);

Step 7:Modify the controller to Store the data to be displayed in view.
public ActionResult Index()
{
ViewBag.mysitename = "www.usmtechworld.com";
ViewBag.myyoutubelink = "https://www.youtube.com/channel/UCA55tUIE-JYb_2Bi1m_eeQQ/featured?disable_polymer=1";
ViewBag.myinstagramlink = "https://www.instagram.com/happycoding2020/";
return View();
}

Step 8: Now add View by right click of View() in Index().

Step 9:Modify the view part as below:-
<div>
<div style="text-align:center;border:1px solid red">
<h1>MySite - ViewBag Example<h1>
<div>
<br >
<table>
<tr>
<td>Site Name:<td>
<td id="t1">@ViewBag.mysitename<td>
<tr>
<tr>
<td>Youtube Name:<td>
<td id="t2">@ViewBag.myyoutubelink<td>
<tr>
<tr>
<td>Instagram:<td>
<td id="t3">@ViewBag.myinstagramlink<td>
<tr>
<table>
<div>
Step 10: Run the code, Your output will be like the below screen