Questions tagged [viewdata]

ViewData is a dictionary used in C# MVC to pass data from the controller to the view. This feature was first introduced in MVC 1.0. To use this tag, the post must be using C#, MVC, and must be in regards to the use of ViewData.

ViewData is one of the alternate ways to pass data from controller to view without using the Model.

Syntax

ViewData is a dictionary that is set in the controller and is read in the view. For example:

Controller

public ActionResult ViewTest()
{
    ViewData["Content"] = "some text";
    return View();
}

View

<div>
    ViewData content: @ViewData["Content"]
</div>

Result

<div>
    ViewData content: some text
</div>

ViewData only accepts string as keys, otherwise it will generate an error alert. So the following is not possible:

ViewData[1] = "some text"; //cannot convert from 'int' to 'string'

As far as a value, these are totally legal:

ViewData["Content"] = 1;            //prints 1
ViewData["Content"] = true;         //prints True
ViewData["Content"] = DateTime.Now; //prints the date (i.e. 4/16/2019 9:50:05 AM)

If the value is a object, you must cast the ViewData in the view in order to access the property to read from:

Controller

public ActionResult ViewTest()
{
    ViewData["Content"] = new SomeClass
    {
        SomeProp = "some text"
    };
    return View();
}

View

<div>
    @{
        var viewDataContent = (SomeClass) ViewData["Content"];
    }
    ViewData content: @viewDataContent.SomeProp
</div>

Because you must cast an object, you cannot use an anonymous type as a value. While a workaround is possible, it's not recommended as it results in ugly code that is difficult to maintain.

Life

The life of ViewData is only for the current request. Once the request is done, ViewData expires. This means that if ViewData is set before a redirect, it will be expired by the time you get to the view:

Controller

public ActionResult ViewRedirect()
{
    ViewData["Content"] = "some text";
    return RedirectToAction("ViewTest");
}

public ActionResult ViewTest()
{
    return View(); // ViewData["Content"] expires
}

View

<div>
    ViewData content: @ViewData["Content"]
</div>

Result

<div>
    ViewData content:
</div>

References

321 questions
12
votes
3 answers

Populating a dropdown from ViewData

I have viewdata in my controller which is populated by a list: List tempEmpList = new List(); tempEmpList = context.employees.ToList(); ViewData["tempEmpList"] = tempEmpList; and I am passing this into my view, the question is,…
JustAnotherDeveloper
  • 3,167
  • 11
  • 37
  • 68
10
votes
2 answers

Pass Additional ViewData to DisplayFor Template

I want to pass an object Model.AvailableVerticalType along with the expression and templateName in the call to the HTML Helper DisplayFor. Without passing the object, the DisplayFor() syntax looks like this: @Html.DisplayFor(o => offer,…
Multi stack
  • 311
  • 3
  • 9
  • 18
10
votes
1 answer

How do I pass multiple models to partial views in ASP.NET MVC

I've been reading Scott Guthrie's post on Passing ViewData from Controllers to Views, but I don't think the lesson is clicking for my specific situation. (Note: Due to client proprietary restrictions, I can't talk paste the actual code, so I…
JasCav
  • 34,458
  • 20
  • 113
  • 170
9
votes
4 answers

How do you use usercontrols in asp.net mvc that display an "island" of data?

I am trying to find out how to use usercontrols in asp.net mvc. I know how to add a usercontrol to a view and how to pass data to it. What I haven't been able to figure out is how do you do this without having to retrieve and pass the data in…
dtc
  • 10,136
  • 16
  • 78
  • 104
9
votes
3 answers

How to pass value by ViewData to Editor Template by @Html.EditorFor

In the view strongly typed view against @model System.Tuple> I use inside a for-each loop: @Html.EditorFor(x => survey.Questions) to render questions in a `Survey. It works flawless. Now I would also like to pass additional…
Yoda
  • 17,363
  • 67
  • 204
  • 344
9
votes
4 answers

MVC User Controls + ViewData

Hi im new to MVC and I've fished around with no luck on how to build MVC User Controls that have ViewData returned to them. I was hoping someone would post a step by step solution on how to approach this problem. If you could make your solution very…
Ayo
  • 1,198
  • 2
  • 16
  • 36
9
votes
3 answers

ASP.NET MVC - How to pass an Array to the view?

I'm struggling myself here, to find a easy way to pass an array from the controller to the view on ASP.NET MVC framework. so in my controller I would have something like: public class HomeController : ApplicationController { public…
zanona
  • 12,345
  • 25
  • 86
  • 141
8
votes
3 answers

Thunderdome MVC- Why one-model-in in MVC?

When Jeremy & Chad posted about their FubuMvc project, one of the differentiators they mentioned was their "Thunderdome Principal": The “Thunderdome Principle” – All Controller methods take in one ViewModel object (or zero objects in some…
Troy
  • 1,640
  • 10
  • 16
8
votes
2 answers

How share ViewData between ViewComponent in Asp.net core

I have two ViewComponent and i want use ViewData or other technic for share some data between and then use this data in main view, but this is not the way, and ViewData per ViewComponent is null when rich to if condition for both…
Mohammad Akbari
  • 4,486
  • 6
  • 43
  • 74
8
votes
1 answer

What is the best way to create dropdownlists in MVC 4?

I want to know,What is a best way to create dropdownlists in MVC 4? With ViewBag or another approach?
7
votes
2 answers

Using ViewData to pass string from Controller to View in ASP.NET MVC3

I am trying to pass a random string from my Controller to the View. Here is my Controller code: [HttpPost] public ActionResult DisplayForm(UserView user) { //some data processing over here ViewData["choice"] = "Apple"; …
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
7
votes
4 answers

C# Centralizing repeating VIewData in MVC

When a user log in into my application i want to show his name throughout the whole application. I am using the asp.net MVC framework. But what i don't want is that is have to put in every controller something like: ViewData["User"] =…
Martijn
  • 24,441
  • 60
  • 174
  • 261
7
votes
2 answers

Adding new line within a textarea that's using asp.net razor markup

How do I add a newline after each item, in a textarea that's inside a Razor @foreach statement? The code below displays everything on one line like... 12341524345634567654354487546765 When I want... 12341524 34563456 76543544 87546765