Reputation: 151
Please let me what I'm doing wrong here. I'm guessing this something to with but not quite sure.
var updateNewSKU = function(make) {
var listItems= "";
for (var i = 0; i < make.length; i++){
listItems += "<option value='" + i + "'>" + i + "</option>";
}
$("select#NewSKU").html(listItems);
}
updateNewSKU(@ViewBag.NewSKUDrop);
In the console, I see the detailed error as;
updateNewSKU(System.Collections.Generic.List`1[System.String]);
Upvotes: 1
Views: 1726
Reputation: 24957
Because ViewBag.NewSKUDrop
content is a List<string>
collection, you need to encode it to JSON string using Json.Encode()
method:
var newSKUDrop = @Html.Raw(Json.Encode(ViewBag.NewSKUDrop));
Or use JSON.parse()
method after @Html.Raw()
:
var newSKUDrop = JSON.parse('@Html.Raw(ViewBag.NewSKUDrop)');
Then you can use the JSON-encoded list to pass into JS method:
updateNewSKU(newSKUDrop);
Note that Razor will execute ToString()
when ViewBag
property contains IEnumerable
, hence the type name System.Collections.Generic.List<System.String>
passed instead.
Addendum: If you're using NewtonSoft JSON library, use JsonConvert.Serialize
to convert the ViewBag
contents as JSON string.
Upvotes: 1