Reputation: 3626
I'm not even sure this is possible with MVC Razor, but I would like to pass my Dictionary that includes another Dictionary to my view and display the child Dictionary keys and values.
public Dictionary<int, dynamic> Getdata(DateInfo dataInfo)
{
//Create a parent dictionary
Dictionary<int, dynamic> parentDict = new Dictionary<int, dynamic>();
//Load the child dictionary
for (int i = 0; i < list.Count; i++)
{
//Create a child dictionary to store all values
Dictionary<int, dynamic> dict = new Dictionary<int, dynamic>();
parentDict[i] = dict;
parentDict[i].Clear();
if (beginningYear < DateTime.Now.Year)
{
//...code left out for brevity
if (NumberOfYears > 1)
{
for(int j = 1; j < NumberOfYears; j++)
{
beginningYear = beginningYear + 1;
//...code left out for brevity
dict.Add(beginningYear, new { Month = 12, MonthlyAmount = nextYearAmount.premium, TotalYearAmount = TotalYearAmount });
}
}
else
{
//...code left out for brevity
}
}
return parentDict;
My Parent Dictionary values looks like this:
[0] = {[0, System.Collections.Generic.Dictionary`2[System.Int32,System.Object]]}
[1] = {[1, System.Collections.Generic.Dictionary`2[System.Int32,System.Object]]}
My Child Dictionary values like this:
[0] { Month = 5, MonthlyAmount = 99.90, TotalYearAmount = 499.50 }
[1] { Month = 12, MonthlyAmount = 399.90, TotalYearAmount = 1499.50 }
[2] { Month = 12, MonthlyAmount = 499.90, TotalYearAmount = 1794.50 }
[0] { Month = 9, MonthlyAmount = 999.90, TotalYearAmount = 6499.50 }
[1] { Month = 12, MonthlyAmount = 3.90, TotalYearAmount = 39.50 }
Within the view:
@foreach (var item in Model.MyDictionary[0])
{
@item.Value
}
That code will display the child value, which is:
{ Month = 5, MonthlyAmount = 99.90, TotalYearAmount = 499.50 }
Is it possible to reference Month, MonthlyAmount, TotalYearAmount?
@item.Value.Month
will not work. 'object' does not contain a definition for 'Month'
And I would like to go through the parent dictionary to reference the child. If I use:
@foreach (var item in Model.MyDictionary[1])
{
@item.Value
}
That will display
{ Month = 9, MonthlyAmount = 999.90, TotalYearAmount = 6499.50 }
This code will not work, but I would like to get the values such as:
@foreach (var item in Model.MyDictionary[0][1])
{
@item.Value.TotalYearAmount
}
and the value displays: 1499.50
Any advice is appreciated.
Upvotes: 0
Views: 1457
Reputation: 4603
Change your line
dict.Add(beginningYear, new { Month = 12, MonthlyAmount = nextYearAmount.premium, TotalYearAmount = TotalYearAmount });
with usage of ExpandoObject to
dynamic expandoObject = new ExpandoObject();
expandoObject.Month = 12;
expandoObject.MonthlyAmount = nextYearAmount.premium;
expandoObject.TotalYearAmount = TotalYearAmount;
dict.Add(beginningYear, expandoObject);
If your Dictionary is longer, you can use following link to convert it in a method: http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/
Upvotes: 1
Reputation: 26634
You can use dynamic
instead of var
like this:
@foreach (dynamic item in Model.MyDictionary[0][1])
{
@item.Value.TotalYearAmount
}
Upvotes: 0