Convert Array To Object

Posted by Sharpcnet under jQuery on 5/7/2014 | Points: 10 | Views : 1657 | Status : [Member] | Replies : 6
Trying to bind a list of objects to a dropdownlist, after PageLoad.
Step 2 works fine and I get the data.
I'm stuck at step 1 and 3.

sCountry is the id of the dropdownlist.

Assuming step 3 is correct, now what should be done in the 'success' part of step 1.

STEP 1 - JS : //Store data in a global object, so that it can be retrieved multiple times.
-------------

var GlobalObjects = { Countries: null,

GetCountries: function () {
if (GlobalObjects.Countries == null) {
$.ajax({
type: "POST",
url: "default.aspx/CountriesData",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (res) {
//What should be done here
},
failure: function (res) {
alert(res.message);
}
});
}
return GlobalObjects.Countries;
}
};


STEP 2 - CS : //Get the data
--------------
[WebMethod]
public static CountryDAL[] CountriesData()
{
CountryDAL cDAL = new CountryDAL();
DataTable dt = cDAL.getdata();
List<CountryDAL> lst = new List<CountryDAL>();

foreach (DataRow dr in dt.Rows)
{
CountryDAL obj = new CountryDAL();
obj.CountryId = Convert.ToInt32(dr["CountryId"]);
obj.CountryName = Convert.ToString(dr["CountryName"]);

lst.Add(obj);
}

return lst.ToArray();
}


STEP 3 - JS : // Bind records to dropdownlist at page load
--------------
$(document).ready(function () {

//Load Countries
var opt = [];
opt.push("<option value='0'>--Select--</option>");
$(GlobalObjects.GetCountries()).each(function (key, value) {
opt.push("<option value=" + value.CountryId + ">" + value.CountryName + "</option>");
});
$("#sCountry").html(opt.join(''));

});





Responses

Posted by: kgovindarao523-21772 on: 5/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

First create a javascript function above the statement var GlobalObjects.... :
function myfun(item)
{
this.CountryId =item.CountryId;
this.CountryName =item.CountryName;
}


now, in your success section in STEP 1
GlobalObjects.Countries=[];
$.each(res,function(index,item){
GlobalObjects.Countries.push(new myfun(item));
});

Now, your step 3 works fine.


Thank you,
Govind

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 5/8/2014 [Member] Starter | Points: 25

Up
0
Down
Hi,
Tried your solution. Did not work.
When I run GlobalObjects.Countries in the browser console, it shows 'null'.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 5/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
This is the solution,
Just debug it whether it is getting into success part in STEP1.




Thank you,
Govind

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 5/8/2014 [Member] Starter | Points: 25

Up
0
Down
Yes it does process the success part. I put alert(res.d.length) after your code and the alert pops up with a number

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 5/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Then Try like this

GlobalObjects.Countries=[];
$.each(res.d,function(index,item){
GlobalObjects.Countries.push(new myfun(item));
});


Thank you,
Govind

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chandradev819 on: 5/9/2014 [Member] Starter | Points: 25

Up
0
Down
Hi

You keep the break point in javascript then check it what error is throwing. You can also bind the dropdownlist like this

 <script type="text/javascript">

$(document).ready(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/BindDatatoDropdown",
data: "{}",
dataType: "json",
success: function(data) {
$.each(data.d, function(key, value) {
$("#ddlCountry").append($("<option></option>").val(value.CountryId).html(value.CountryName));
});
},
error: function(result) {
alert("Error");
}
});
});
</script>


like this post

http://www.aspdotnet-suresh.com/2012/07/how-to-bind-dropdownlist-in-aspnet.html
http://www.codeproject.com/Tips/688228/Bind-Dropdownlist-in-ASP-NET-using-jQuery-AJAX


Thanks and Regards
Chandradev
My Blog

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response