lundi 10 mars 2014

Send color hex from controller to view without quotes becomes "?


Vote count:

0




I am struggling to send through a value and color pair with JSON, the color value HAS to be returned to the javascript in the view as color: "#FFFFFF" for example, and I can send it to the view like that, but the second the browser reads it it becomes color: "#FFFFFF" which doesn't work. This is a snippet of my code:



values.Add(new StudentBrandsApp.Models.StatsValues() { value = Convert.ToInt32(dr.ItemArray[1].ToString()), color = "#F38630" });

// populate with some values.
var serializer = new JsonSerializer();
var stringWriter = new StringWriter();
var writer = new JsonTextWriter(stringWriter);
writer.QuoteName = false;
serializer.Serialize(writer, values);
writer.Close();
var json = stringWriter.ToString();
json = HttpUtility.HtmlEncode(json);
ViewData["json"] = json;


And in my javascript:



<script src="~/Scripts/Chart.js"></script>
<script type="text/javascript">

@{string jsonstring = ViewData["json"].ToString();}
var data = @jsonstring;

//Get the context of the canvas element we want to select
var ctx = document.getElementById("myChart").getContext("2d");
var myNewChart = new Chart(ctx).Pie(data);
//Get context with jQuery - using jQuery's .get() method.
var ctx = $("#myChart").get(0).getContext("2d");
//This will get the first returned node in the jQuery collection.
var myNewChart = new Chart(ctx);
new Chart(ctx).Pie(data, options);
</script>


Here is the chart.js documentation on how values and colors should be represented for pie charts: http://ift.tt/133wzl3



asked 3 mins ago


1 Answer



Vote count:

2




You need to write @Html.Raw(jsonString) to prevent Razor from automatically escaping it.



answered 54 secs ago

SLaks

368k




Aucun commentaire:

Enregistrer un commentaire