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
-1
votes
1 answer

set Javascript parameters as key in viewdata,tempdata?

"Hi I am new to MVC so sorry if this sounds silly ,but please could anyone tell me how to set javascript parameters as keys in viewdata or tempdata.. the problem is that i want to set display property of certain elements in my view any want them to…
-1
votes
1 answer

Create ViewData outside Controler in MVC

I have a method inside a controller that creates ViewData. For example this one private void CreateFKViewData(OSQDCOL osqdcol, OSADCOL osadcol, IList targetTBLcols) { ... ViewData[osadcol.ColName] = new SelectList(SelectListItems,…
Christoph Adamakis
  • 865
  • 2
  • 9
  • 26
-2
votes
1 answer

Dynamic column name in ViewBag or ViewData in asp net mvc

I want to write column name dynamically in a collection. My collection is in ViewBag and in ViewData. I am trying @{foreach (var item in ViewData["example"] as List ) { …
oneNiceFriend
  • 6,691
  • 7
  • 29
  • 39
-2
votes
3 answers

Mvc 4 - How to pass parameters to a partial view with viewbag

I have a _Layout page and in this _Layout page, I am calling a Partial View as below.
@{Html.RenderAction("_Account", "Home");}
_Account.cshtml ... @if (User.Identity.IsAuthenticated) {
doganilker
  • 103
  • 2
  • 16
-2
votes
1 answer

Extract tempdata or viewdata value in javascript?

how do i extract the value of tempdata or viewdata in javascript? This returns "" even though there is a value being set. Is the syntax incorrect? I've tried without the '' as well as casting to string. Nothing seems to work... var message =…
newbie_86
  • 4,520
  • 17
  • 58
  • 89
1 2 3
21
22