c# - Passing dictionary of colours to view MVC -
i trying retrieve dictionary of colours on model view. getting error dictionary of colours can't serialized. in model create list follows.
public dictionary<int, color> colourlist = new dictionary<int, color>();
i create list in model
public dictionary<int, color> createcolourpalette() { colourlist.add(1, system.drawing.colortranslator.fromhtml("#f2dcdb")); colourlist.add(2, system.drawing.colortranslator.fromhtml("#e6b8b7")); colourlist.add(3, system.drawing.colortranslator.fromhtml("#da9694")); colourlist.add(4, system.drawing.colortranslator.fromhtml("#c20046")); colourlist.add(5, system.drawing.colortranslator.fromhtml("#d8e4bc")); colourlist.add(6, system.drawing.colortranslator.fromhtml("#c4d79b")); colourlist.add(7, system.drawing.colortranslator.fromhtml("#76933c")); colourlist.add(8, system.drawing.colortranslator.fromhtml("#b7dee8")); colourlist.add(9, system.drawing.colortranslator.fromhtml("#92cddc")); colourlist.add(10, system.drawing.colortranslator.fromhtml("#4f81bd")); colourlist.add(11, system.drawing.colortranslator.fromhtml("#ccccff")); colourlist.add(12, system.drawing.colortranslator.fromhtml("#b1a0c7")); colourlist.add(13, system.drawing.colortranslator.fromhtml("#711471")); colourlist.add(14, system.drawing.colortranslator.fromhtml("#eeece1")); colourlist.add(15, system.drawing.colortranslator.fromhtml("#ddd9c4")); colourlist.add(16, system.drawing.colortranslator.fromhtml("#c4bd97")); colourlist.add(17, system.drawing.colortranslator.fromhtml("#494529")); colourlist.add(18, system.drawing.colortranslator.fromhtml("#00aeef")); return colourlist; }
in view following, user clicks button , calls function creates table, errors on json.encode line, colourlist populated correctly when view won't serialise, missing?
function createtable() { var num_cols = 0; var headings = new array(); headings.push("cost type"); var colours = @html.raw(json.encode(model.colourlist)); var checkbox = $("input[name=selectedyears]"); (var = 0; < checkbox.length; i++) { if (checkbox[i].checked) { var chkboxtext = checkbox[i].nextsibling; if (chkboxtext != null) headings.push(chkboxtext.nodevalue); } } var num_cols = headings.length; var theader = '<table border="1">\n'; var tbody = ''; //create heading row tbody += '<tr>'; (var j = 0; j < headings.length; j++) { tbody += '<td style="margin-right:10px;">'; tbody += headings[j].tostring(); tbody += '</td>' } var costtypes = $("input[name=selectedcosttypes]") tbody += '</tr>\n'; for( var i=0; i<costtypes.length;i++) { if (costtypes[i].checked) { var chkcosttypetext = costtypes[i].nextsibling; if (chkcosttypetext != null) { tbody += '<tr>'; tbody += '<td>'; tbody += chkcosttypetext.nodevalue; tbody += '</td>' tbody += '<td>'; tbody += colours[i]; tbody += '</td>' tbody += '</tr>\n'; } } } var tfooter = '</table>'; document.getelementbyid('wrapper').innerhtml = theader + tbody + tfooter; }
this error.
type 'system.collections.generic.dictionary`2[[system.int32, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089],[system.drawing.color, system.drawing, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a]]' not supported serialization/deserialization of dictionary, keys must strings or objects.
the problem dictionary cannot have non-string keys because won't understood correctly javascript. fix in case seems simple enough - turn dictionary 1 uses string representation of numbers keys:
public dictionary<string, color> colourlist = new dictionary<string, color>(); public dictionary<string, color> createcolourpalette() { colourlist.add("1", system.drawing.colortranslator.fromhtml("#f2dcdb")); ...
Comments
Post a Comment