C# 通过颜色字典查看MVC
我正试图从我的角度检索模型上的颜色词典。但是我得到一个错误,颜色字典不能被序列化。在我的模型中,我创建了如下列表C# 通过颜色字典查看MVC,c#,javascript,jquery,asp.net-mvc,asp.net-mvc-4,C#,Javascript,Jquery,Asp.net Mvc,Asp.net Mvc 4,我正试图从我的角度检索模型上的颜色词典。但是我得到一个错误,颜色字典不能被序列化。在我的模型中,我创建了如下列表 public Dictionary<int, Color> Colourlist = new Dictionary<int, Color>(); 问题是您的字典不能有非字符串键,因为javascript无法正确理解它们。然而,在您的情况下,修复似乎足够简单-只需将您的字典转换为使用数字的字符串表示作为键的字典: public Dictionary<st
public Dictionary<int, Color> Colourlist = new Dictionary<int, Color>();
问题是您的字典不能有非字符串键,因为javascript无法正确理解它们。然而,在您的情况下,修复似乎足够简单-只需将您的字典转换为使用数字的字符串表示作为键的字典:
public Dictionary<string, Color> Colourlist = new Dictionary<string, Color>();
public Dictionary<string, Color> CreateColourPalette()
{
Colourlist.Add("1", System.Drawing.ColorTranslator.FromHtml("#f2dcdb"));
...
public Dictionary colorlist=new Dictionary();
公共词典CreateColor调色板()
{
colorlist.Add(“1”,System.Drawing.ColorTranslator.FromHtml(“#f2dcdb”);
...
很抱歉,但是你真的需要学习一些JS。你目前编写的代码有点糟糕。我对javascript不太熟悉,所以它可能不是最好的,但是我认为堆栈溢出是为需要帮助的人准备的
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.
public Dictionary<string, Color> Colourlist = new Dictionary<string, Color>();
public Dictionary<string, Color> CreateColourPalette()
{
Colourlist.Add("1", System.Drawing.ColorTranslator.FromHtml("#f2dcdb"));
...