0

i have bind drop down using WebMethod (Services) I have multiple drop down list and need to bind on mutully dependent to each other like cascading of dropdown list but while passing value for one selected vale its showing null while debugging on my web method

WebMethod:

 public static List<Company> GetCompanies(string CountryCode, string CompanyCode)
        {
            GetInitiatorList.MasterDataServiceClient oClient = new GetInitiatorList.MasterDataServiceClient();
            Almarai.GiveAway.GetInitiatorList.ALM_COMPANY_M[] initiatorsList = oClient.GetActiveCompanies(CountryCode, CompanyCode)                                                                                 List<Company> Companyes = new List<Company>();
            foreach (Almarai.GiveAway.GetInitiatorList.ALM_COMPANY_M company in initiatorsList)
            {
                Companyes.Add(new Company()
                {
                    CountryCode = company.CompanyCode,
                    CompanyName = company.CompanyName
                });
            }
            return Companyes;
        }

JQuery

 $('#ddlCountry').change(function (e) {

   var selectedCountry = $('#ddlCountry').val();
   $.ajax({
            type: "POST",
            url: "Header.aspx/GetCompanies",
            data: JSON.stringify({ CountryCode: selectedCountry, CompanyCode: 'SAU' }),
            dataType: "json",
            contentType: "application/json",
            success: function (res) {
            $.each(res.d, function (data, value) {
            $("#ddlCompany").append($("<option></option>").val(value.CompanyId).html(value.CompanyName));

            $('#ddlCompany').change(function (e) {

Aspx:

 <div class="col-md-8">
       <select id="ddlCountry">
          <option value="0">--Select Country--</option>
       </select>
 </div> 

enter image description here Where i am doing wrong Please guide me

1
  • In the web browser, use F12 tools and network tab to work out what the ajax is posting.
    – Nick.Mc
    Commented Sep 28, 2020 at 10:11

4 Answers 4

1

To get the currently selected value from dropdown:

$('#ddlCountry').val();

To get the currently selected text from dropdown:

$('#ddlCountry :selected').text();

You can check first your ajax call is working properly or not , by sending hard coded text by specifying like this in ajax call:

$.ajax({
    type: "POST",
    url: "Header.aspx/GetCompanies",
    data: JSON.stringify({ CountryCode: 'USA', CompanyCode: 'SAU' })
    ....

Then you can check are you assign dropdown values properly or not ? If you only want text not value then you can use as I have mentioned on top. I think it will solve your problem. Please let me know if it isn't.

============ Updated (after reading comments) ======================

I have created a sample demo based on your sample. As I don't have the service code as you have so it is very simple to give it a test to check either it is working or not or it will give me the same error as like yours.

What I have done for testing this may be look trivial to you but please check , it works for me and give me right result. here is screen shot of Route Config file , take a look at the red circle ( I have to change this for web method works)

RouteConfig file image

Screen shot of chrome dev tools ( for Contact.aspx page here for simplicity) Chrome dev tools screen shot

And finally Contact.aspx -> GetCompanies web method break point hit screen shot , here i have modified method body for simplicity , just to check data binding is working or not.

enter image description here

So , it's work for me. Would you please check all of your step one by one and let me know. If it will help you then it will be my pleasure.

12
  • Hard coded value is working but $('#ddlCountry').val(); is not working Commented Sep 28, 2020 at 11:51
  • Iam binding these drop down list using services Commented Sep 28, 2020 at 12:23
  • Then would you check your services value in debugger , are those okay or not ? Commented Sep 29, 2020 at 11:01
  • I checked its coming even though i have bind 1st drop down list successfully i want those values which already bind from 1st drop down list Commented Sep 29, 2020 at 11:06
  • @user14304386 I have re-edited my answer , please check and let me know. Commented Sep 29, 2020 at 14:00
0

I think you don't need to stringify it.

Instead of:

data: JSON.stringify({ CountryCode: selectedCountry, CompanyCode: 'SAU' }),

try:

data: { CountryCode: selectedCountry, CompanyCode: 'SAU' },
1
  • Added but not its not firing web method Commented Sep 28, 2020 at 10:24
0

Very silly point I miss out here From web Method I Was returning CompanyCode as you can see

CountryCode = company.CompanyCode, CompanyName = company.CompanyName

And In Html I was trying to bind CompanyId as you can see here :

$("#ddlCompany").append($("<option></option>").val(value.CompanyId).html(value.CompanyName));

So it was coming null or 0 for obvious region.

I Just replace value.CompanyId to CompanyCode and its now working perfectly

0

Do you need .aspx suffix?

Try like this url: "Header/GetCompanies", 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.