c# - Displaying values from a Dictionary within a Dictionary within MVC Razor view -
i'm not sure possible mvc razor, pass dictionary includes dictionary view , display child dictionary keys , values.
public dictionary<int, dynamic> getdata(dateinfo datainfo) { //create parent dictionary dictionary<int, dynamic> parentdict = new dictionary<int, dynamic>(); //load child dictionary (int = 0; < list.count; i++) { //create child dictionary store values dictionary<int, dynamic> dict = new dictionary<int, dynamic>(); parentdict[i] = dict; parentdict[i].clear(); if (beginningyear < datetime.now.year) { //...code left out brevity if (numberofyears > 1) { for(int j = 1; j < numberofyears; j++) { beginningyear = beginningyear + 1; //...code left out brevity dict.add(beginningyear, new { month = 12, monthlyamount = nextyearamount.premium, totalyearamount = totalyearamount }); } } else { //...code left out brevity } } return parentdict;
my parent dictionary values looks 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 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 view:
@foreach (var item in model.mydictionary[0]) { @item.value }
that code display child value, is:
{ month = 5, monthlyamount = 99.90, totalyearamount = 499.50 }
is possible reference month, monthlyamount, totalyearamount?
@item.value.month
will not work. 'object' not contain definition 'month'
and go through parent dictionary reference child. if use:
@foreach (var item in model.mydictionary[1]) { @item.value }
that display
{ month = 9, monthlyamount = 999.90, totalyearamount = 6499.50 }
this code not work, values such as:
@foreach (var item in model.mydictionary[0][1]) { @item.value.totalyearamount }
and value displays: 1499.50
any advice appreciated.
change line
dict.add(beginningyear, new { month = 12, monthlyamount = nextyearamount.premium, totalyearamount = totalyearamount });
with usage of expandoobject
dynamic expandoobject = new expandoobject(); expandoobject.month = 12; expandoobject.monthlyamount = nextyearamount.premium; expandoobject.totalyearamount = totalyearamount; dict.add(beginningyear, expandoobject);
if dictionary longer, can use following link convert in method: http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/
Comments
Post a Comment