Harshida Parmar

Harshida Parmar

  • NA
  • 10
  • 1.1k

Viewbag in Javascript Uncaught SyntaxError: Unexpected token <

Aug 27 2021 5:22 AM

I am fetching the data using API and assign it to VIEW BAG but at run time it shows me an error.

List<DevInfo> DevList = await RestApi.Instance.GetAllDevAsync();
var nameT = DevList.Select(a=>a.Name).ToList();
ViewBag.datasourceDevList = nameT;

And in the script.

var nameList = <%= new JavaScriptSerializer().Serialize("@ViewBag.datasourceDevList") %>;

I want to generate the dropdown list.

if (nameList != '') {
                var tableHtml;
                $.each(JSON.parse(nameList), function (index, value) {
                    //tableHtml += "<option value=" + value.Name + ">" + value.Name + "</option>";
                    console.log(value);
                });
                /*$("#selectTrainer").html(tableHtml);*/
                $("#selectTrainer").append(tableHtml);
}

Answers (1)