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
- Html
- *******************
- @{
- var contractClass = ViewData["ContractClass"] as List<SelectListItem>;
- if (contractClass == null || contractClass.Count == 0)
- {
- @Html.DropDownList("ddlContract", new List<SelectListItem>(), "--Select A Contract--", new { disabled = true, id = "ddlContract", @style = "width:95%; disabled" })
- }
- else
- {
- @Html.DropDownList("ddlContract", contractClass, "--Select A Contract--", new { id = "ddlContract", @style = "width:95%" })
- }
- }
- </td>
- </tr>
- <tr id="myWdtoToHide" style="display: none;">
- <td>
- <span id=wdtoDrop>WD/TO</span> <br>
- <select name=ddlwdto id=ddlwdto style="HEIGHT: 25px; WIDTH: 95%">
- <option value="" selected>--Select A WD/TO--</option>
- </select>
-
- </td>
- </tr>
- <tr id="cdrlselect">
- <td>
- <span id=cdrlDrop>CDRL</span> <br>
- <select name=ddlCdrl id=ddlCdrl disabled style="HEIGHT: 25px; WIDTH: 95%">
- <option value="" selected>--Select A CDRL Number--</option>
- </select>
-
- </td>
- </tr>
- <tr>
- <td id="DDID">
- <span id=scheduleDrop>CDRL Due Date</span> <br>
- <select name=ddlSchedules id=ddlSchedules style="HEIGHT: 25px; WIDTH: 95%">
- <option value="" selected>--Select A Due Date--</option>
- </select>
-
- </td>
- <td> </td>
- </tr>
- </tbody>
- </table>
- JQuery
- $('#ddlwdto').change(function ()
- {
-
- CascadeClearCdrl();
-
- var contractId = window.contractID;
- if (!contractId) return;
- var wdtoSelected = $('#ddlwdto').prop('selectedIndex') > 0;
- if (wdtoSelected) {
- var cdrlwdtoId = $('#ddlwdto').val();
- window.wdtoID = cdrlwdtoId;
- LoadCdriSelect();
- }
- });
- function LoadCdriSelect() {
- $.ajax({
- method: "GET",
- dataType: "json",
- url: "@Url.Action("LoadCdrls", "VendorUpload")",
- cache: false,
- data: { contractid: window.contractID, wdtoid: window.wdtoID },
- success: function (result) {
-
- $.each(result, function (i, cdrls) {
- if (window.wdtoID) {
- debugger;
- $("#ddlCdrl").append('<option value="' + cdrls.Value + '">' + cdrls.Text + '</option>');
-
- }
- //$("#ddlCdrl").append('<option value="' + cdrls.Value + '">' + cdrls.Text + '</option>');
- });
- $("#ddlCdrl").removeAttr("disabled");
- },
- error: function() {
- alert('CDRL retrieval Error.');
- }
- });
- }
- function CascadeClearCdrl() {
- var control = $("#ddlCdrl");
- ClearDropDown(control);
- CascadeClearSchedule();
- }