top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What are the various ways to send data from a controller to view?

+1 vote
What are the various ways to send data from a controller to view?
posted May 24, 2017 by Jdk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
Best answer

ViewData and ViewBag

We saw how Models and ViewModels can be used to pass data from controller to the view. But now consider scenario where we need to pass small amount of data from controllers to view. Now for every such case if we start creating ViewModels then managing these view models will become a nightmare. For all such requirements where we need to transfer small amount of data, we can use Viewdata and ViewBag.


ViewData is an in built dictionary object that can be accessed and set with string type key values. It is derived from ViewDataDictionary class and is a very handy data structure when it comes to passing data from Controller to View. The data is only alive for one request and cannot persist between request. The only problem with ViewData is that the type casting is required for complex data types at the location where the data is being extracted.


C# 4.0 has the possibility of having dynamic Variables. ViewBag data structure has been created to utilize this and is written as a wrapper over the ViewData so that the type casting for complex objects will not be required if we use ViewBag. ViewBag comes with all the benefits of ViewData with an additional benefit that type casting is not required.


Now the problem with all the above ways of data transfer was that the data is only alive for current request. The data is lost if a redirection takes place i.e. one Action redirects to another action. For the scenarios where we need to persist the data between actions/redirection another dictionary object called

can be used. It is derived from TempDataDictionary. It is created on top of session. Its will live till the redirected view is fully loaded.

Session is the way to persist the data till the current session is alive. If we need some data to be accessible from multiple controllers, actions and views then Session is the way to store and retrieve the data.

answer Jul 11, 2017 by Shivaranjini
Similar Questions
0 votes

I am working on a Web Api application and below is the razor templates used in my code.

enter image description here

The _layout.cshtml page contains footer which needs to be shared across the other templates. Below is the footer to be shared across templates.

<p> This is footer </p>

I have tried using the following to share the footer but it is not rendering the page.

 @model SR.Services.External.Data.EmailNotificationModel
<body style=\"color:#808080;font-size:8pt;font-family:Arial\">
        Hi @Model.TargetUserName,
        You have been removed from community '@Model.ObjectName' by @Model.Initiator.


Any help is appreciated.

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.