I used the following in my MVC3 (aspx) to generate an array from ViewData that contains a list:
<script runat="server">
void RenderJavascriptHashArray (string listName, string hashName)
{
IEnumerable<KeyValuePair<int, string>> pairs = this.ViewData[listName] as IEnumerable<KeyValuePair<int, string>>;
Response.Write("var " + hashName + " = new Array();\n");
foreach (KeyValuePair<int, string> pair in pairs)
{
Response.Write(hashName + "[" + pair.Key.ToString() + "] = \"" + pair.Value + "\";\n");
}
}
</script>
<script>
<% this.RenderJavascriptHashArray("InfoURLs", "InfoURLs"); %>
</script>
In MVC5 Razor, the array is displayed on the screen because of Response.Write, is there an alternative approach! Would appreciate your suggestions.
Try this i do not think you need response if you are in razor.