Reputation: 1413
I have a 2D Array declared in my MVC Controller, and I need to access this via Razor so I can loop through each value.
I create a session and set it as the array, but I can't figure out how to access the array through razor.
Controller:
string[,] Things = new string[,] {
{ "thing1", "pie" },
{ "thing1", "cake" },
{ "thing1", "potato" }
};
public void GetThings()
{
Session["Things"] = Things;
}
public ActionResult Index()
{
GetThings();
return View();
}
Razor:
@{
for (int i = 0; i < Session["Things"].GetLength(0); i++)
{
@i
}
}
I get the error "'object' does not contain a definition for Getlength
, the only suggested actions are .Equals
, .GetHashCode
, .GetType
, and .ToString
.
The above c# in the razor works if I declare the array within the razor, replacing "Session..." with the array variable name.
I can't read any values from the array session to display on the HTML front end, doing @Session["Things"]
displays System.String[,]
in browser (but then this is the same as if I tried to call the array declared in razor), @Session["Things"][1,1]
gives browser error
Cannot apply indexing with [] to an expression of type 'object'
Upvotes: 0
Views: 393