2013-07-21 48 views
0

我想從我的視圖檢索我的模型上的顏色字典。但是我得到一個顏色字典無法序列化的錯誤。在我的模型中,我創建如下列表。傳遞顏色字典查看MVC

public Dictionary<int, Color> Colourlist = new Dictionary<int, Color>(); 

我在模型

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; 
     } 

在我這樣做的視圖中創建的列表這樣的,基本上用戶點擊按鈕,它調用這個函數創建一個表,它在JSON的錯誤.Encode行,當我查看它時,Colourlist會正確填充,但它不會連續出現,我錯過了什麼?

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]"); 
      for (var i = 0; i < 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>'; 
      for (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; 
     } 

這是錯誤。

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]]' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects. 
+0

[這裏看看,請(http://stackoverflow.com/questions/11749103/html-rawjson-encode-and-internet-explorer-9) – Ohgodwhy

+0

我很抱歉地說,但你真的需要學習一些JS。你目前寫的代碼有點可怕。 –

+0

我是JavaScript新手,所以是可能不是最好的,但我認爲堆棧溢出是需要幫助的人 – user329540

回答

0

問題是你的字典不能有非字符串鍵,因爲它們不能被javascript正確理解。然而,在你的情況下解決似乎是很簡單 - 只是把你的字典到使用數字的字符串表示的關鍵之一:

public Dictionary<string, Color> Colourlist = new Dictionary<string, Color>(); 

public Dictionary<string, Color> CreateColourPalette() 
{ 
    Colourlist.Add("1", System.Drawing.ColorTranslator.FromHtml("#f2dcdb")); 
    ...