How do I convert a C# List<string[]> to a Javascript array?

bynary :

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 class AddressModel
    public string Addresses { get; set; }

My controller action looks like the following:

AddressModel lAddressGeocodeModel = new AddressGeocodeModel();
List<string[]> lAddresses = new List<string[]>();

string lSQL = " select Address1, CityName, StateCode, ZipCode " +
                      " from AddressTable  ";

// Convert the data to a List to be serialized into a Javascript array.
//{ retrieval code goes here...
foreach (DataRow row in AddressTable.Rows)
    string[] lAddress = new string[5];
    lAddress[1] = row["Address1"].ToString();
    lAddress[2] = row["CityName"].ToString();
    lAddress[3] = row["StateCode"].ToString();
    lAddress[4] = row["ZipCode"].ToString();

lAddressGeocodeModel.UnitCount = lAddresses.Count().ToString();
// Here I'm using the Newtonsoft JSON library to serialize my List
lAddressGeocodeModel.Addresses = JsonConvert.SerializeObject(lAddresses);

return View(lAddressModel);

Then in my view I get the following string of addresses:

[["123 Street St.","City","CA","12345"],["456 Street St.","City","UT","12345"],["789 Street St.","City","OR","12345"]]

How am I supposed to get this serialized string residing in a razor model into a javascript array?

Dustin Kingen :

You could directly inject the values into JavaScript:

<script type="text/javascript">
    var arrayOfArrays = JSON.parse('@Html.Raw(Model.Addresses)');

See JSON.parse, Html.Raw

Alternatively you can get the values via Ajax:

public ActionResult GetValues()
    // logic
    // Edit you don't need to serialize it just return the object

    return Json(new { Addresses: lAddressGeocodeModel });

<script type="text/javascript">
$(function() {
        type: 'POST',
        url: '@Url.Action("GetValues")',
        success: function(result) {
            // do something with result

See jQuery.ajax

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at


Login to comment


How do I convert a javascript object array to a string array of the object attribute I want?

How do I convert a Python list into a C array by using ctypes?

How do I convert a string to a list of chars?

How do I convert a Swift Array to a String?

How do I convert an array list to hash separated string?

How do i convert a List<Interface> to List<Class> in c#

How do i convert a character array to a string?

How do I convert a TreeMap that is a <String, <List<String>> to String[][]?

How do i convert a String to a List<Map<String,String>>

how do I convert a list of custom type to a list of String

How do I convert a Json file (that contains a array) to a List (of a class object) C# Unity

How do I convert an unsigned char array into a string in C?

How do I convert this php array into a JavaScript array of objects?

How to convert List in c# to array in javascript and check the array item

How do I take the string returned from gen_results_table in PHP SDK and convert it to a Javascript array?

How do I convert string array values to object in c#

How do I convert an array into an object within array - Angular and Javascript

How do I convert 1-dimensional array of String to String?

How do I convert string builder to string array in Android?

How do I convert a list represented as a string to a list?

How do I convert a string into a string array String()?

How can I convert string to array in JavaScript?

How do I convert a string to an array?

How do I convert an array or a list into a hashref?

How do I convert a nested array to a 'keyed' array in JavaScript?

How do I convert List<String?> to List<String> in .NET 6 / C# 10?

how do i Convert the C to javascript

How I convert string response into Array List

How do i convert this string into an array type in - Javascript