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

putra1908

I have list in model and the item from database. I want put the item to array in java script. then when I put values in textbox and click the button the system check from the array the values have in array or not

when I try this code, its not work. Any ideas or suggestion?

Model:

public List<string> listponumber { get; set; }

public void getlist()
        {
            var listpo = new List<string>();
            SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            connection.Open();

            SqlCommand cmdponumber = new SqlCommand("Select DISTINCT ponumber From t_transaction ORDER BY ponumber", connection);

            SqlDataReader rdr = cmdponumber.ExecuteReader();
            while (rdr.Read())
            {
                listpo.Add(rdr.GetString(0).ToString());
            }

            connection.Close();
            listponumber = new List<string>(listpo);
        }

View:

<script type="text/javascript">

    $(document).ready(function () {

        $("#btntest").click(function () {
            var ponumber = [@Model.listponumber]
            var txtvalues = document.getElementByID("textbox").value;
            if (ponumber.indexOf(txtvalues) > -1 ) {
                alert('Array');
            } else { 
              alert('not array');
            }
        });
</script>
jcruzz92

You can convert the C# List into a Javascript array by using

<script>
    var javascriptArray = @Html.Raw(Json.Encode(Model.ListToConvertToJavascript));
</script>

This will create a Json object with the data inside the List in your Model and you will be able to iterate through it in plain Javascript.

The syntax error might you get is the IDE telling you that you are using a syntax that the Javascript parser doesn't understand, which is the RAZR sentece @Html(...); you can ignore it.

Test it out using:

for(int i = 0; i < $(javascriptArray).lenght; i++){
    alert(javascriptArray[i]);
}

or opening your browser developer's console and typing the name of the array you are creating to see if it is populated by your Model List data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if item is in an array / list

how to check if item exists in a javascript array

How to convert List in c# to array in javascript .net core

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

check for the last item in a javascript array

check if an array "includes" an item in Javascript

Convert array to list in javascript

Convert list to array in JavaScript

How to convert a python list of tuples to an array in JavaScript?

How to convert a DOM node list to an array in Javascript?

How to convert javascript array to specific list of objects

how to convert json to array in javascript for an li list

How to convert word list to array in JavaScript

REACT Todo-List : How to check if an item already exist in the array

C# how to check array in list

How to check if an item is already there in an array

How to check if an item in an array is a letter?

How to convert Json array to list in c#

How to convert a JSON array into a C# List?

How to convert Array of Strings with space into single Array list in Javascript?

How to check if there are two (or more) forward slashes in a string of an Array item with Javascript

Convert Java List to Javascript Array

How to convert array to array of array in Javascript?

Check if an item of an array is in a string using Regex in JavaScript

Javascript check if array item has class error

How to add an item to an array of array list

How to check if array item matches array item in liquid (Shopify)

check if a list of array contains an array in c#

Convert list to array in c#