How do I convert a C# List to a Javascript array?

前端 未结 9 920
情书的邮戳
情书的邮戳 2020-11-27 16:56

I have a datatable that I\'m converting into a List, serializing it and passing it to my view using a viewmodel.

My viewmodel looks like this:

public         


        
9条回答
  •  星月不相逢
    2020-11-27 17:41

    For one dimension array

    Controller:

    using Newtonsoft.Json;
    var listOfIds = _dbContext.Countries.Where(x => x.Id == Country.USA).First().Cities.Where(x => x.IsCoveredByCompany).Select(x => x.Id).ToList();
    string strArrayForJS = JsonConvert.SerializeObject(listOfIds); //  [1,2,6,7,8,18,25,61,129]
    //Now pass it to the view through the model or ViewBag 
    

    View:

    
    

提交回复
热议问题