Moons
Moons

Reputation: 3854

Handling a ArrayList or List type of C# in jquery

I want to bind the HTML select element with the database.

I want to bind the text and value of select element

The problem is dont want to return the string array with text and value of each option combined using some symbol rather i want to return an array list or some List

Now after returning using PageMEthods i cannot iterate over the result to get the id and value of select options.

I cannot use JSON i have to use PageMethods only.

ArrayList obj = new ArrayList();

var countries = EntityService.ServiceInstance.GetCountries();

foreach (var country in countries)
{
    ListItemClass temp = new ListItemClass();

    temp.ListItemText = country.country_name.ToString();
    temp.ListItemValue = country.id.ToString();

    obj.Add(temp);
}

return obj;

What I want is how can I get the ID and country name in jQuery

Any help is appreciated.

Thanks

PROBLEM SOLVED:

I need to write :

where test is the return type of method

$.each(test,
    function (intIndex, objValue) {
        $.each(objValue, function (intIndex1, objValue1) {
            alert(objValue1);
        })
    }
);

But dont know whether this is a correct approach or not.

Upvotes: 0

Views: 1760

Answers (3)

Moons
Moons

Reputation: 3854

Finally this worked for me:

Where result is the returned using page methods.

THough this might not be the best way.

function bind_select(result, el_name) {

$.each(result,
         function (intIndex, objValue) {

             var option_text;
             var option_value;

             $.each(objValue,
         function (object_property, objValue1) {

             if (object_property == "ListItemText") {
                 option_text = objValue1;
             }
             else {
                 option_value = objValue1;
             }
         })

             $('#' + el_name).append($("<option></option>").
              attr("value", option_value).
              text(option_text));

         })
  }

Any other ideas are appreciable

Thanks.

Upvotes: 0

Niranjan Singh
Niranjan Singh

Reputation: 18290

<select id='list'>
    <option value='1'>India</option>
    <option value='2'>US</option>
    <option value='3'>UK C</option>
</select>

Select by Jquery:

$("#list option:selected").text(); will return country name

get Value

$("#list option:selected").val(); will return country value

Get the value when you change selection:

$('#list').change(function() {
   // assign the value to a variable, so you can test to see if it is working
    var selectVal = $('#list :selected').val();
var selectText = $('#list :selected').text();

});

Now it depend upon you which method you choose to send value server side.

.ajax()
.post()

Check this links for help: jQuery-AJAX calling ASP.NET page method. How to return value back to jQuery? http://weblogs.asp.net/karan/archive/2010/09/12/calling-server-side-method-using-jquery-ajax.aspx

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

I like to use json serialized object (DTO) server side.. choose your method to send data if you not privileged to use JSON.

Check this Using complex types to make calling services less… complex

Upvotes: 1

Dave Walker
Dave Walker

Reputation: 3523

Try returning a strongly typed Array rather than an ArrayList. Also don't return as that returntype - return as a better DTO like Country with a name and an ID.

You should then be able to see the properties on the returned object if you have a look at it using firebug. You can set a breakpoint - or explicitly use 'debugger;' to view it at the point you want to start iterating over it.

Upvotes: 0

Related Questions