Hi i just wonder is it possible to put 2 ajax under 1 function? at the moment i got something like this for population: but the population only work for one box not two but after i select a country it will work but i want it to pick up the default value
from database for both drop down but only one of it working. any idea?
it should work both. What is the javascript that it is not working? And what does mean"does not work"? Do you have any javascript errors?
Do you always return a value true/false from JsonResult to see evetually errors on server? http://bit.ly/mvc_ajax_jquery
There is no error it just don't pick up the default value from my database. but when i select country it work. but this is my edit pages so i want it to pick up the default value that saved on my database. any idea?
window.alert after each statement to see if not javascript errors somewhere.
done the alart but only one of the alert will come out when i do refresh. :S and its AreaNo alarm pop out. which always sucess populate data from database but the City don't.
BlackShadows
Member
4 Points
28 Posts
mvc3 json n ajax problem
May 01, 2012 03:39 PM|LINK
Hi i just wonder is it possible to put 2 ajax under 1 function? at the moment i got something like this for population: but the population only work for one box not two but after i select a country it will work but i want it to pick up the default value from database for both drop down but only one of it working. any idea?
<script type="text/javascript">
$(function () {
PopulateCountry();
$('#CountryCode').change(function () {
PopulateFromCountry($(this).val());
});
PopulateCity();
$('#CityCode').change(function () {
PopulateFromCity($(this).val());
});
PopulateAreaNo();
$('#AreaCode').change(function () {
PopulateFromAreaNo($(this).val());
});
function PopulateCountry() {
$.ajax({ url: '/Test/CountryList/',
type: 'POST',
dataType: 'json',
success: function (data) {
var options = $('#Country');
$.each(data, function () {
options.append($('<option />').val(this.CountryCode).text(this.CountryName));
});
var theOriginalValue = $(options).attr('TheOriginalValue');
var startValue = "";
if (theOriginalValue != "") {
// alert(theOriginalValue);
$(options).val(theOriginalValue);
startValue = theOriginalValue;
}
else {
startValue = $('option:first', options).val();
}
PopulateFromCountry(startValue);
}
});
}
function PopulateFromCountry(AreaNo) {
$.ajax({ url: '/Test/AreaNoList/',
type: 'POST',
data: { AreaNo: AreaNo },
dataType: 'json',
success: function (data) {
var options = $('#AreaNo');
$('option', options).remove();
$.each(data, function () {
options.append($('<option />').val(this.AreaNo).text(this.AreaTeleNumber));
});
var theOriginalValue = $(options).attr('TheOriginalValue');
var startValue = "";
if (theOriginalValue != "") {
// alert(theOriginalValue);
$(options).val(theOriginalValue);
startValue = theOriginalValue;
}
else {
startValue = $('option:first', options).val();
}
PopulateFromAreaNo(startValue);
}
});
}
function PopulateFromCountry(CityCode) {
$.ajax({ url: '/Test/CityList/',
type: 'POST',
data: { CityCode: CityCode },
dataType: 'json',
success: function (data) {
var options = $('#CityCode');
$('option', options).remove();
$.each(data, function () {
options.append($('<option />').val(this.CityCode.text(this.CityName));
});
var theOriginalValue = $(options).attr('TheOriginalValue');
var startValue = "";
if (theOriginalValue != "") {
// alert(theOriginalValue);
$(options).val(theOriginalValue);
startValue = theOriginalValue;
}
else {
startValue = $('option:first', options).val();
}
PopulateFromCity(startValue);
}
});
}
});
</script>
But only one pupulation will work so i wonder is is possible to put the function into something like this
"function PopulateFromCountry(CityCode, AreaNo)
ajax for city
ajax for Area"
Thank.
ignatandrei
All-Star
134511 Points
21576 Posts
Moderator
MVP
Re: mvc3 json n ajax problem
May 01, 2012 04:32 PM|LINK
it should work both. What is the javascript that it is not working? And what does mean"does not work"? Do you have any javascript errors?
Do you always return a value true/false from JsonResult to see evetually errors on server? http://bit.ly/mvc_ajax_jquery
BlackShadows
Member
4 Points
28 Posts
Re: mvc3 json n ajax problem
May 02, 2012 08:16 AM|LINK
There is no error it just don't pick up the default value from my database. but when i select country it work. but this is my edit pages so i want it to pick up the default value that saved on my database. any idea?
ignatandrei
All-Star
134511 Points
21576 Posts
Moderator
MVP
Re: mvc3 json n ajax problem
May 02, 2012 08:22 AM|LINK
How the default value it is picked ? What is the code ? ( Minimum, please)
BlackShadows
Member
4 Points
28 Posts
Re: mvc3 json n ajax problem
May 02, 2012 08:30 AM|LINK
it will pick from my database with those code:
var theOriginalValue = $(options).attr('TheOriginalValue');
var startValue = "";
if (theOriginalValue != "") {
// alert(theOriginalValue);
$(options).val(theOriginalValue);
startValue = theOriginalValue;
}
else {
startValue = $('option:first', options).val();
}
PopulateFromCity(startValue);
}
});
}
it work for my AreaNo, but not the CityCode. the code are same but it just pick up. but after a country is selected it will populate it.
ignatandrei
All-Star
134511 Points
21576 Posts
Moderator
MVP
Re: mvc3 json n ajax problem
May 02, 2012 09:28 AM|LINK
put
window.alert after each statement to see if not javascript errors somewhere.
asteranup
All-Star
30184 Points
4906 Posts
Re: mvc3 json n ajax problem
May 02, 2012 09:41 AM|LINK
Hi,
If you are trying for cascading dropdownlist, you can check the following post-
http://forums.asp.net/p/1723443/4610124.aspx/1?Re+Unable+to+get+data+list+from+server+code+
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
BlackShadows
Member
4 Points
28 Posts
Re: mvc3 json n ajax problem
May 02, 2012 09:47 AM|LINK
i done the cascading part it just don't pick up my default value from my database.
BlackShadows
Member
4 Points
28 Posts
Re: mvc3 json n ajax problem
May 02, 2012 09:48 AM|LINK
done the alart but only one of the alert will come out when i do refresh. :S and its AreaNo alarm pop out. which always sucess populate data from database but the City don't.
BlackShadows
Member
4 Points
28 Posts
Re: mvc3 json n ajax problem
May 02, 2012 10:07 AM|LINK
nvm found a way for it:
<script type="text/javascript">
$(function () {
PopulateCountry();
$('#CountryCode').change(function () {
PopulateFromCountry1($(this).val());
PopulateFromCountry2($(this).val());
});
PopulateCity();
$('#CityCode').change(function () {
PopulateFromCity($(this).val());
});
PopulateAreaNo();
$('#AreaCode').change(function () {
PopulateFromAreaNo($(this).val());
});
function PopulateCountry() {
$.ajax({ url: '/Test/CountryList/',
type: 'POST',
dataType: 'json',
success: function (data) {
var options = $('#Country');
$.each(data, function () {
options.append($('<option />').val(this.CountryCode).text(this.CountryName));
});
var theOriginalValue = $(options).attr('TheOriginalValue');
var startValue = "";
if (theOriginalValue != "") {
// alert(theOriginalValue);
$(options).val(theOriginalValue);
startValue = theOriginalValue;
}
else {
startValue = $('option:first', options).val();
}
PopulateFromCountry1(startValue);
PopulateFromCountry2(startValue);
}
});
}
function PopulateFromCountry1(AreaNo) {
$.ajax({ url: '/Test/AreaNoList/',
type: 'POST',
data: { AreaNo: AreaNo },
dataType: 'json',
success: function (data) {
var options = $('#AreaNo');
$('option', options).remove();
$.each(data, function () {
options.append($('<option />').val(this.AreaNo).text(this.AreaTeleNumber));
});
var theOriginalValue = $(options).attr('TheOriginalValue');
var startValue = "";
if (theOriginalValue != "") {
// alert(theOriginalValue);
$(options).val(theOriginalValue);
startValue = theOriginalValue;
}
else {
startValue = $('option:first', options).val();
}
PopulateFromAreaNo(startValue);
}
});
}
function PopulateFromCountry2(CityCode) {
$.ajax({ url: '/Test/CityList/',
type: 'POST',
data: { CityCode: CityCode },
dataType: 'json',
success: function (data) {
var options = $('#CityCode');
$('option', options).remove();
$.each(data, function () {
options.append($('<option />').val(this.CityCode.text(this.CityName));
});
var theOriginalValue = $(options).attr('TheOriginalValue');
var startValue = "";
if (theOriginalValue != "") {
// alert(theOriginalValue);
$(options).val(theOriginalValue);
startValue = theOriginalValue;
}
else {
startValue = $('option:first', options).val();
}
PopulateFromCity(startValue);
}
});
}
});
</script>
The bold text is what i changed or added