I have an ASP.NET MVC 4 app. This app has a view with a block of javascript in the footer. I'm using the RAZOR view engine. I'm trying to figure out how to convert my result set into a JSON array. Currently, I'm doing the following:
<script type="text/javascript">
@for (int i=0; i<ViewBag.States.Count; i++) {
<text>
{ id: @Html.Raw(ViewBag.States[i]["ID"], name: \"" + ViewBag.States[i]["Name"] + "\"}")
</text>
if (i < (ViewBag.States.Count - 1)) {
<text>,</text>
}
}
</script>
This approach seems syntactically sloppy. However, I'm not sure how else to do it. Does anyone know how I can clean this up? With this approach, I can't even figure out how to wrap the "ID" value in quotes.