Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
//{
...data 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();
    lAddresses.Add(lAddress);
}

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?

like image 389
bynary Avatar asked Aug 27 '13 16:08

bynary


People also ask

How do you convert F to C easily?

To convert temperatures in degrees Fahrenheit to Celsius, subtract 32 and multiply by . 5556 (or 5/9).

What converts AC to DC current?

The device that is used to convert AC to DC is a rectifier. As the name suggests, a rectifier corrects or straightens the direction of the current. This device periodically reverses the direction of alternating current (AC) to direct current (DC), which usually flows in just one direction.

What temp is Celsius in Fahrenheit?

1 Celsius is equal to 33.8 Fahrenheit.

Why do we convert AC to DC?

AC power cannot be stored but DC power can be stored. In order to store electrical energy alternating current is converted to direct current. Digital devices require direct current to work. Was this answer helpful?


2 Answers

You could directly inject the values into JavaScript:

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

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() {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("GetValues")',
        success: function(result) {
            // do something with result
        }
    });
});
</script>

See jQuery.ajax

like image 112
Dustin Kingen Avatar answered Oct 13 '22 00:10

Dustin Kingen


Many way to Json Parse but i have found most effective way to

 @model  List<string[]>

     <script>

         function DataParse() {
             var model = '@Html.Raw(Json.Encode(Model))';
             var data = JSON.parse(model);  

            for (i = 0; i < data.length; i++) {
            ......
             }
         }
     </script>
like image 38
Mehul Bhalala Avatar answered Oct 13 '22 02:10

Mehul Bhalala