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

mvc+razor trying to render just parts of the model

I'm trying to render two tables via one model. I'm using my helper dll to create the tables. the problem is, I don't know how I can render only parts of my viewdata, at the first call of the page. here is some code: Controller: (Creating two tables…
Daniel
  • 31
  • 5
0
votes
0 answers

Using ViewData to send requested URL to View for hyperlink, URL truncates parameter

I'm using a filter that checks the user's browser/version upon arrival to the site. If they use an unsupported browser, I save the URL they intended to reach into a ViewData called "RequestedURL" and redirect to a view telling them their browser is…
0
votes
1 answer

Keeping track of loaded scripts per page using ViewData

I'm currently trying to find a good way to load my javascript files only when I need them. In order to do this, I created several HtmlHelpers for faceboxlinks, datepickerfields, tinymcefields and other scripts that need an external js and an…
Leon
  • 139
  • 6
0
votes
2 answers

How to create a DropDownList from an encapsulated property from a dynamically created ViewBag?

Note: Variables names were changed to keep my code's purpose anonymous. Task: Create a SelectList that will show what was chosen (if anything was chosen), and will generate a list to choose from. ViewBag Declaration / Definition in the CarShow…
0
votes
2 answers

How to assign ViewData value as div id in asp.net mvc

hi friends help me to get out of this.. //controller { ViewData["idvalue"]="id"+1; return View(); } // Viewpage
id has to set as id1.. help me, how i can set div id as id1..? thanks in advance. regards,
Cooldharma06
  • 515
  • 1
  • 6
  • 24
0
votes
2 answers

How to access results of dynamic linq query (PredicateBuilder linq to entities) in a partial view in MVC 4?

I have a controller action that uses PredicateBuilder to build a dynamic linq query. I want to pass the results of this query to a partial view. What is the best way to do this? If it's best practice to always use strongly typed views, should my…
MattSull
  • 5,514
  • 5
  • 46
  • 68
0
votes
1 answer

ViewData in ActionFilterAttribute - Object reference not set to an instance of an object

I've been trying to load masterpage content from database (based on a "TargetCode" in the querystring), using ActionFilterAttribute. However, I'm having problem setting the ViewData to the data returned from the database, here is the code: public…
Xuan Vu
  • 247
  • 1
  • 4
  • 15
0
votes
1 answer

Assign Value to ViewData in Razor

I would like to store some user inputs into a cookie on the client side. Using MVC3/Razor. Data entered into several DropDownLists. Examople of one below:
@Html.Telerik().DropDownList() …
mad moe
  • 322
  • 3
  • 14
0
votes
1 answer

How to Preserve Value of JQuery Search in ASP.Net MVC

I have an MVC view where I have a search box. On keyup, I perform a JQuery search and render the results to a div>