Reputation: 39
I,m trying to pass value from jquery to webmethod. but when ever 1 pass value webmethod does not starts. and when i remove the passing value code it starts working properly What is the problem? JS code.
$(function () {
$.ajax({
type: "POST",
url: "ContryCityDDL.aspx/GetCountry",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var ddlcontry = $("[id*=ddlcontry]");
ddlcontry.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(r.d, function () {
ddlcontry.append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
});
$("#ddlcontry").change(function () {
alert($('option:selected', $(this)).val());
var countyId = $(this).val();
$("#HiddenField1").val($('option:selected', $(this)).val());
$.ajax({
type: "POST",
url: "ContryCityDDL.aspx/GetCity&countryId='"+countryId+"'",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var ddlcity = $("[id*=ddlcity]");
ddlcity.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(r.d, function () {
ddlcity.append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
});
});
});
C#
[WebMethod]
public static List<ListItem> GetCity(string countryId)
{
string constrng;
string query = "Select CityId,CityName From City";
//string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
constrng = "Data Source=(LocalDB)\\v11.0;AttachDbFilename='C:\\Users\\Abdul Basit Mehmood\\Desktop\\DropdownAJAX\\DropdownAJAX\\App_Data\\ContryCity.mdf';Integrated Security=True";
using (SqlConnection con = new SqlConnection(constrng))
{
using (SqlCommand cmd = new SqlCommand(query))
{
List<ListItem> customers = new List<ListItem>();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new ListItem
{
Value = sdr["CityId"].ToString(),
Text = sdr["CityName"].ToString()
});
}
}
con.Close();
return customers;
}
}
}
when i remove 'string countryId'
from c# code and `var countyId = $(this).val();&countryId='"+countryId+"'" from js the code runs properly.
Upvotes: 0
Views: 41
Reputation: 235
try this on ddlcontry change function,
$("#ddlcontry").change(function () {
alert($('option:selected', $(this)).val());
var countryId = $(this).val();
$("#HiddenField1").val($('option:selected', $(this)).val());
$.ajax({
type: "POST",
url: "ContryCityDDL.aspx/GetCity",
data: "{'countryId':'" + countryId + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var ddlcity = $("[id*=ddlcity]");
ddlcity.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(r.d, function () {
ddlcity.append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
});
});
hope this is working.
Upvotes: 0
Reputation: 77876
You want to pass the ContryId
as query string from your ajax call and in that case use ?
character and not &
like
"ContryCityDDL.aspx/GetCity?countryId='"
Upvotes: 2