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

Pass viewdata to a create form MVC3

I passed the id from one controller (Index) to another (Details), and added a ViewData statement to save data in the details view, and forward it to a View on another controller. @Html.EditorFor(model => model.PersonID) How do I modify this…
hamackey
  • 51
  • 1
  • 4
0
votes
3 answers

Filter Excel Output using MVC 3

I am trying to filter the rows that are sent to a excel file. I have been able to figure out how to export row data to excel, but now I need to send filtered data to an excel sheet since I added a version dropdown. What happens is I select the…
Goldentp
  • 187
  • 1
  • 7
  • 28
0
votes
1 answer

MVC - Passing Parameters from View to Popup

I'm having a problem passing parameters from my view to my popup. In my view, I have the following razor code to render an Action Link wherein when I click it, a pop-up will appear: @Html.ActionLink("[Edit Product]", "Edit", "Products", new {…
Smiley
  • 3,207
  • 13
  • 49
  • 66
0
votes
1 answer

How to change labels of a form with json and razor snytax in .Net MVC3

I'm trying to use json for my web page's globalization options.. id like to change the labels of my form just by using a little dropdownbox and without refreshing the whole page and more interesting part is i got more than two form in my view. so…
Berker Yüceer
  • 7,026
  • 18
  • 68
  • 102
0
votes
1 answer

Can't access ViewDataDictionary object properly on StringTemplate

I'm playing with StringTemplate version 3.1.4.6002 as a view engine for an MVC project. I can't seem to access an object being passed to the template and have it render on a webpage. So basically, I have the the following in the controller (in C#):…
Noble_Bright_Life
  • 569
  • 3
  • 9
  • 16
0
votes
2 answers

Passing ViewData versus making a new class

I've seen several posts on StackOverflow and elsewhere discussing the various ways to bring together data from multiple entities into a strongly typed view, these being using the ViewData object or constructing a new custom class that utilizes both…
Graham Conzett
  • 8,344
  • 11
  • 55
  • 94
0
votes
1 answer

Whats in a model? Why we need to use it

Have one Doubt In MVC Architecture we can able to pass data from Controller to Directly view without creating strongly typed view, than why there is need to use Models ..? Is it decreases the Performance of the application if we are saving data in…
dLcreations
  • 357
  • 2
  • 14
0
votes
1 answer

Refreshing ViewData in a method which returns Json

I have a controller with a method which returns a partial view and generates ViewData. Then, I have some methods, each of them returns Json objects and ViewData. But, the ViewData is not getting refreshed. How to refresh it? Is this possible? public…
petko_stankoski
  • 10,459
  • 41
  • 127
  • 231
0
votes
2 answers

Passing Querystring data to a Model in ASP.NET MVC 2

I have a strongly typed view inheriting from a POCO class. I want to initialize the property of a model with a Querystring value at the time when view loads. On the View Load I am using ViewData to the save the code : public ActionResult Data() {…
Kunal
  • 1,913
  • 6
  • 29
  • 45
0
votes
3 answers

Completely not understanding View Data for MVC right now, need some serious help & code examples

I really need to maintain this string called "filterParams" in my MVC application. After the user enters some search parameters he clicks submit, and the grid is rebinded with that parameter. That works great. I also save the filterParams data in a…
0
votes
1 answer

View Models (ViewData), UserControls/Partials and Global variables - best practice?

I'm trying to figure out a good way to have 'global' members (such as CurrentUser, Theme etc.) in all of my partials as well as in my views. I don't want to have a logic class that can return this data (like BL.CurrentUser) I do think it needs to be…
elado
  • 8,510
  • 9
  • 51
  • 60
0
votes
1 answer

telerik grid ForeignKey binding filter

I have a grid that displays the Orders from customers, it contains an ItemID in it. Now, im using the telerik grid for mvc to perform CRUD operations on the second grid, for the column of ItemID i use ForeignKey with no problems just like this…
0
votes
1 answer

Refresh viewData object in renderPartial

I need help with this.. I have a viewdata with a list of clients and i feed it when the Index action method is called. /* Index action is the method that open the view. */ Once that the Index method was called, the variable is feeded and view is…
Fausto Carasai
  • 251
  • 1
  • 6
  • 16
-1
votes
1 answer

Want to pass data from the controller to the view WITHOUT adding it to the URL

I'm using ASP.Net MVC 5, and I want to pass data from the controller to the view WITHOUT adding it to the URL. I've tried it like this: public ActionResult Index(LoginViewModel loginViewModel) { var landingPgVm = new LandingPgViewModel(); …
Scott Fraley
  • 378
  • 4
  • 14
-1
votes
2 answers

Save variable value while navigation in MVC

I am having one page ABC.cshtml. In the page I have written code for multiple modal popup with "Back" and "Next" buttons. when I click on buttons it navigates from one modal to another. I am having "Id" which I need to persist when I navigate from…
1 2 3
21
22