From ViewBag to JSON

0

I am passing a controller ViewBag to the view and would like to pass it to a numeric array in JavaScript.

The content of the ViewBag:

  

"12,13,17,18,19,8,9,14"

My last attempt has been this:

var horas = @Html.Raw(Json.Encode(ViewBag.HorasBloquear));
var array = JSON.parse(horas);

when making the parse I get the error:

  

VM253: 1 Uncaught SyntaxError: Unexpected token, in JSON at position 2

Does anyone think of how to pass the ViewBag to a numeric array?

    
asked by Borja Calvo 20.10.2016 в 10:52
source

2 answers

0

I can not prove it but I think this will work for you:

var jss = new System.Web.Script.Serialization.JavaScriptSerializer(); 
var horasBloquear= jss.Serialize(ViewBag.HorasBloquear);

var array =  JSON.parse('@Html.Raw(horasBloquear)');
    
answered by 20.10.2016 в 11:12
0

I've already managed to get the array easily.

var horas = @Html.Raw(Json.Encode(ViewBag.HorasBloquear));
var arrayBloqueo = horas.split(',');
    
answered by 20.10.2016 в 11:50