Selected Item in dropdown showing All instead of SelectedIndex Changed

Selected Item in dropdown showing All instead of SelectedIndex Changed

I have one dropdown which is optional and need to show only up when there is data and is selected. When is selected, the next dropdown should only show data related to the  selected item. For some reason I can see the selected ID  but my Jquery function is not returning the matching item, rather returning everything because of the "data: { contractid: window.contractID }". I want to add extra parameter so that if that parameter is present, the related item should be only loaded. Below is my html and the JQuery

  1. Html
  2. *******************

  3. @{
  4. var contractClass = ViewData["ContractClass"] as List<SelectListItem>;
  5. if (contractClass == null || contractClass.Count == 0)
  6. {
  7. @Html.DropDownList("ddlContract", new List<SelectListItem>(), "--Select A Contract--", new { disabled = true, id = "ddlContract", @style = "width:95%; disabled" })
  8. }
  9. else
  10. {
  11. @Html.DropDownList("ddlContract", contractClass, "--Select A Contract--", new { id = "ddlContract", @style = "width:95%" })
  12. }
  13. }
  14. </td>
  15. </tr>
  16. <tr id="myWdtoToHide" style="display: none;">
  17. <td>
  18. <span id=wdtoDrop>WD/TO</span> <br>
  19. <select name=ddlwdto id=ddlwdto style="HEIGHT: 25px; WIDTH: 95%">
  20. <option value="" selected>--Select A WD/TO--</option>
  21. </select>
  22.  
  23. </td>
  24. </tr>
  25. <tr id="cdrlselect">
  26. <td>
  27. <span id=cdrlDrop>CDRL</span> <br>
  28. <select name=ddlCdrl id=ddlCdrl disabled style="HEIGHT: 25px; WIDTH: 95%">
  29. <option value="" selected>--Select A CDRL Number--</option>
  30. </select>
  31.  
  32. </td>
  33. </tr>
  34. <tr>
  35. <td id="DDID">
  36. <span id=scheduleDrop>CDRL Due Date</span> <br>
  37. <select name=ddlSchedules id=ddlSchedules style="HEIGHT: 25px; WIDTH: 95%">
  38. <option value="" selected>--Select A Due Date--</option>
  39. </select>
  40.  
  41. </td>
  42. <td>&nbsp; </td>
  43. </tr>
  44. </tbody>
  45. </table>





  46. JQuery

  47. $('#ddlwdto').change(function ()
  48.  {
  49.   
  50.    CascadeClearCdrl();
  51.    
  52.    var contractId = window.contractID;
  53.    if (!contractId) return;
  54.    var wdtoSelected = $('#ddlwdto').prop('selectedIndex') > 0;
  55.    if (wdtoSelected) {
  56.        var cdrlwdtoId = $('#ddlwdto').val();
  57.        window.wdtoID = cdrlwdtoId;
  58.        LoadCdriSelect();
  59.    }
  60. });



  61. function LoadCdriSelect() {
  62. $.ajax({
  63. method: "GET",
  64. dataType: "json",
  65. url: "@Url.Action("LoadCdrls", "VendorUpload")",
  66. cache: false,
  67. data: { contractid: window.contractID, wdtoid: window.wdtoID },
  68. success: function (result) {
  69.  
  70.    $.each(result, function (i, cdrls) {
  71.        if (window.wdtoID) {
  72.            debugger;
  73.            $("#ddlCdrl").append('<option value="' + cdrls.Value + '">' + cdrls.Text + '</option>');
  74.            
  75.        }
  76. //$("#ddlCdrl").append('<option value="' + cdrls.Value + '">' + cdrls.Text + '</option>');
  77. });
  78. $("#ddlCdrl").removeAttr("disabled");
  79. },
  80. error: function() {
  81. alert('CDRL retrieval Error.');
  82. }
  83. });
  84. }

  85.               function CascadeClearCdrl() {
  86. var control = $("#ddlCdrl");
  87. ClearDropDown(control);
  88. CascadeClearSchedule();
  89. }