Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Every Notch is linked to a selected salarygrade.when you choose a salary grade,the notch must be filtered to that linked salary grade.I am using ajax to do it but it isn't working please help.

What I have tried:

Delphi
<div class="form-group">
                                        @Html.LabelFor(model => model.SalaryGradeId, htmlAttributes: new { @class = "control-label col-md-2" })
                                        <div class="col-md-6">
                                            @Html.DropDownListFor(model => model.SalaryGradeId, (List<SelectListItem>)Model.SalaryGradeList, new { @class = "form-control", onchange = "GetSalaryNotchByGrades();", @id = "SalaryGrade_Cbx" })
                                            @Html.ValidationMessageFor(model => model.SalaryGradeId, "", new { @class = "text-danger" })
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        @Html.LabelFor(model => model.NotchId, htmlAttributes: new { @class = "control-label col-md-2" })
                                        <div class="col-md-6">                                             
                                           <select class="form-control" id="SalaryNotch_Cbx" onchange="GetAmount();">                                                                                                                                      
                                          </select>
                                            @Html.ValidationMessageFor(model => model.NotchId, "", new { @class = "text-danger" })
                                        </div>
                                    </div>


this is the javascript i'm using to pull the information from the api


<script>
    function GetAmount() {

                $.ajax({
                    url: baseUrl + 'api/SalaryNotch/' + $("#SalaryNotch_Cbx").val(),
                    type: 'GET',
                    headers: { 'Access-Control-Allow-Origin': '*' },
                    dataType: 'json',
                    success: function (data) {
                        if (data.HasError == 0) {
                            console.log(data.Model);
                            $("#BasicSalary_Txt").val(data.Model.Monthly)
                        }
                    },
                    error: function (request, error) {
                        console.log(error);
                    }
                });
            }          
        
      

    function GetSalaryNotchByGrades() {

                $.ajax({
                    url: baseUrl + 'api/SalaryGradeByNotch/' + $("#SalaryGrade_Cbx").val(),
                    type: 'GET',
                    headers: { 'Access-Control-Allow-Origin': '*' },
                    dataType: 'json',
                    success: function (data) {
                        if (data.HasError == 0) {
                            console.log(data.Model);
                            $("#SalaryNotch_Cbx").val(data.Model);
                            $("#SalaryNotch_Cbx").html("");
                            $("#SalaryNotch_Cbx").append
                           // ($('<option></option>').val(null).html("Select Notch"));
                            $.each($.parseJSON(data), function (i, notch)
                            { $("#SalaryNotch_Cbx").append($('<option></option>').val(notch.Id).html(notch.Name)) })
                                
                        }
                    },
                    error: function (request, error) {
                        console.log(error);
                    }
                });
            }


</script>
Posted
Comments
F-ES Sitecore 15-Jan-18 5:50am    
"not working" doesn't give anyone enough information to help you. Would you phone a mechanic and say "My car isn't working, how do I fix it?"

For example you are using a relative path to your api ("api/...") how do we know that is valid when we don't have access to your machine? You need to learn how to at least do basic javascript debugging to trace the path the code is taking to find out what it does that you don't want it to do and what it doesn't do that you want it to do.
Karthik_Mahalingam 15-Jan-18 8:59am    
are you getting any error in console window?
Member 13053943 15-Jan-18 16:35pm    
yes,i edited the code and got this error

function GetSalaryNotchByGrades() {

$.ajax({
url: baseUrl + 'api/SalaryGradeByNotch/' + $("#SalaryGrade_Cbx").val(),
type: 'GET',
headers: { 'Access-Control-Allow-Origin': '*' },
dataType: 'json',
success: function (data) {
if (data.HasError == 0) {
console.log(data.Model);
$("#SalaryNotch_Cbx").val(data.Model);
$("#SalaryNotch_Cbx").html("");
$("#SalaryNotch_Cbx").append
($('<option></option>').val(null).html("Select Notch"));
$.each(data,function (i, notch)
{ $("#SalaryNotch_Cbx").append($('<option></option>').val(notch.Id).html(notch.Name)) })

}
},
error: function (request, error) {
console.log(error);
}
});
}




this is the error i got
Uncaught TypeError: Cannot read property 'Id' of null
Karthik_Mahalingam 16-Jan-18 23:56pm    
means notch value is null/undefined

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900