Asp.Net MVC ViewBag TempData ViewData to Send Data from Controller to View

  Total Views : 3152
  Comments : 1
 
  Keywords : How to pass data from controller to view in asp.net mvc, Different ways to pass data from controller to view in asp.net mvc, Tempdata, viewbag, viewdata in asp.net mvc to send data.

Here we will learn viewbag in asp.net mvc, tempdata and viewdata in asp.net mvc with example and how to use viewbag, tempdata and viewdata in asp.net mvc to send or pass data from controller to view with example.

Asp.Net MVC Send Data From Controller to View

We have three options to pass data from controller to view in asp.net mvc application those are 

 

  • TempData
  • ViewBag
  • ViewData

TempData in Asp.Net MVC

TempData in mvc is used to pass data from Controller to Controller or Controller to view and it is used to pass data from the current request to the next request.

 

TempData has short life and it required type casting. Following is the code snippet of TempData in mvc

 

// Summary:

//     Gets or sets the dictionary for temporary data.

//

// Returns:

//     The dictionary for temporary data.

public TempDataDictionary TempData { get; set; }

TempData is a dictionary object that is derived from TempDataDictionary class. We will see how we can assign value to TempData in Controller with simple example for that create one new controller in application and write code like as shown below

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial4.Controllers

{

public class TestController : Controller

{

public ActionResult Index()

{

TempData["test"] = "hi Tempdata";

return View();

}

}

}

Now we will create one view to show data for that right click on controller à select add view give name for that view (Index.cshtml). Open view file and write following code to show data in website

 

@{

    ViewBag.Title = "Index";

 }

 <h2>Index</h2>

@TempData["test"]

 

When we run above code we will get output like as shown below 

 

Tempdata output in asp.net mvc application

ViewData in Asp.Net MVC

ViewData is used to pass data from controller to view and it contains a null value when redirection occurs. ViewData is derived from ViewDataDictionary which is dictionary type and ViewData required type casting. Following is the code snippet of ViewData in MVC

 

// Summary:

// Gets or sets the dictionary for view data.

//

// Returns:

// The dictionary for the view data.

public ViewDataDictionary ViewData { get; set; }

 

ViewData is a dictionary object that is derived from ViewDataDictionary class. We will see how we can assign value to ViewData in Controller with simple example for that create one new controller in application and write code like as shown below

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial4.Controllers

{

public class TestController : Controller

{

public ActionResult Index()

{

ViewData["test"] = "hi Viewdata";

return View();

}

}

}

Now we will create one view to show data for that right click on controller à select add view give name for that view (Index.cshtml). Open view file and write following code to show data in website

 

@{

ViewBag.Title = "Index";

}

<h2>Index</h2>

@ViewData["test"]

 

ViewBag in Asp.Net MVC

ViewBag is used to pass data from controller to view and it does not required type casting. ViewBag contains a null value when redirection occurs and following is the code snippet of viewbag in mvc

 

// Summary:

// Gets the dynamic view data dictionary.

//

// Returns:

// The dynamic view data dictionary.

[Dynamic]

public dynamic ViewBag { get; }

 

Generally ViewBag uses dynamic type which was introduce in visual C# 4.0. We will see how we can assign value to ViewBag in Controller with simple example for that create one new controller in application and write code like as shown below

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial4.Controllers

{

public class TestController : Controller

{

public ActionResult Index()

{

ViewBag.Test = "hi ViewBag";

return View();

}

}

}

Now we will create one view to show data for that right click on controller à select add view give name for that view (Index.cshtml). Open view file and write following code to show data in website

 

@{

ViewBag.Title = "Index";

}

<h2>Index</h2>

@ViewBag.Test

 

By using above methods of TempData, ViewData and ViewBag we can pass data from controller to view in asp.net mvc application.

 Comments (1)

  • tushar chorghe tutlane registered profile image
     
    tushar chorghe   Reply

    very good.

     Aug 20, 2016

Leave a comment

  • Default Tutlane User