简体   繁体   中英

Passing List String from C# to JavaScript

I am currently trying to check if a value of a string variable is "Apple". Now I need to pass a list of fruits to javascript from C#.

C# Code

List<String> fruits = new List<String>{"Apple","Mango","Orange"}

JavaScript Code

$(document).on('click','#dvAppContent input:checkbox[id*=chkfunction]', function () {
   ToggleApplication(this);
});

function ToggleApplication(currentFunction) {
   var fruitName = $(currentFunction).closest('ui').parent('label').text().trim();    
   If(fruitName == "Apple")
   {
     return true;
   }       
}

Use Ajax call in JavaScript. Something like this:

<script>  
    $(document).ready(function () {  
        $.ajax({  
            type: "GET",  
            url: "/api/StudentAPI/GetAllStudents",  
            contentType: "application/json; charset=utf-8",  
            dataType: "json",  
            success: function (data) {  
                //alert(JSON.stringify(data));                  
                $("#DIV").html('');   
                var DIV = '';  
                $.each(data, function (i, item) {  
                    var rows = "<tr>" +  
                        "<td id='RegdNo'>" + item.regNo + "</td>" +  
                        "<td id='Name'>" + item.name + "</td>" +  
                        "<td id='Address'>" + item.address + "</td>" +  
                        "<td id='PhoneNo'>" + item.phoneNo + "</td>" +  
                        "<td id='AdmissionDate'>" + Date(item.admissionDate,  
                         "dd-MM-yyyy") + "</td>" +  
                        "</tr>";  
                    $('#Table').append(rows);  
                }); //End of foreach Loop   
                console.log(data);  
            }, //End of AJAX Success function  
  
            failure: function (data) {  
                alert(data.responseText);  
            }, //End of AJAX failure function  
            error: function (data) {  
                alert(data.responseText);  
            } //End of AJAX error function  
  
        });         
    });  
</script>  

And in the backend in c#, something like this:

public class StudentAPIController : Controller  
   {  
       // GET: api/GetAllStudents  
       [HttpGet]  
       public IEnumerable<PersonalDetail> GetAllStudents()  
       {  
           List<PersonalDetail> students = new List<PersonalDetail>  
           {  
           new PersonalDetail{  
                              RegNo = "2017-0001",  
                              Name = "Nishan",  
                              Address = "Kathmandu",  
                              PhoneNo = "9849845061",  
                              AdmissionDate = DateTime.Now  
                              },  
           new PersonalDetail{  
                              RegNo = "2017-0002",  
                              Name = "Namrata Rai",  
                              Address = "Bhaktapur",  
                              PhoneNo = "9849845062",  
                              AdmissionDate = DateTime.Now  
                             },                
           };  
           return students;  
       }  
   }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM