Search code examples
c#asp.netasp.net-mvcdictionarytempdata

ASP.net MVC Tempdata getting DataSet's values out of it


Hey all I am new to the MVC world and was wanting to get a few values from the TempData that I made that houses 3 DataSet's inside it.

The values that are inside the TempData are these:

enter image description here

then selecting tTrip then rows then Results View:

enter image description here

finally seeing the DataSet values for tTrip:

enter image description here

I populate the TempData like so (simplify):

allData.Merge("tEvents"); //Gathers data and places it into tEvents
allData.Merge("tTrip"); //Gathers data and places it into tTrip
allData.Merge("tExternalTrainingMain"); //Gathers data and places it into tExternalTrainingMain

TempData["jsonData"] = allData;

I've tried:

string blah = TempData["jsonData"][0]["somename"].toString();

string blah = TempData["jsonData"][0].somename.toString();

string blah = TempData["jsonData"][0][0].somename.toString();

string blah = TempData["jsonData"][0][0][0].toString();

But only get errors. What is the proper way of getting the dataset values out of the TempData array?

UPDATE for DavidG

enter image description here

enter image description here


Solution

  • The indexed property that gives you the value from TempData returns a object of type object. You need to cast your value to the desired type. For example:

    var myDataSet = TempData["jsonData"] as DataSet;
    if(myDataSet != null)
    {
        We have a dataset now!
        string blah = myDataSet.Tables[0]["somename"].toString();
    }