0

this is my code to change the drop down list values when the checkbox is checked. I use asp.net mvc framework and java script to call the action. The list of the drop-down should show only the expired medicines when the checkbox is checked. But, it results in (Error: [object:object]). It didn't hit the controller action 'getmedicine'. Can anyone help me?

function GetMedicineList(_isExp) {

    var url = "getmedicine";

    $.ajax({
        url: url,
        data: { IsExp: _isExp },
        cache: false,
        type: "GET",
        dataType: "json",
        success: function (data) {
            var markup = "<option value='0'>Please Select Expired Medicine</option>";
            for (var x = 0; x < data.length; x++) {
                markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
            }
            $("#clientId").html(markup).show();
        },
        error: function (reponse) {
            alert("error : " + reponse);
        }
    });

}

and in the view,

@Html.CheckBoxFor(m => m.IsExp, new { Id = "isExp",  @onchange = "GetMedicineList(this.value);" })
17
  • Have you tried $("#clientId").append(markup) ? Commented Mar 27, 2017 at 8:27
  • 1
    If it didn't hit the controller action, you probably call wrong url. In your example you basically call yourdomain.com/getmedicine. Is it correct? Commented Mar 27, 2017 at 8:28
  • I tried that, but nothing happened. Commented Mar 27, 2017 at 8:30
  • 1
    Check the network tab in your browsers development console and see why the Http request failed. Commented Mar 27, 2017 at 8:34
  • 1
    you could use url = 'URL.Action("ActionName", "ControllerName")' instead of writing the url like url = "GetMedicine". It would generate the url to the action properly. Commented Mar 27, 2017 at 8:54

1 Answer 1

1

I solved the problem. One error is the action that I use to trigger is not an action result. It is a list. I changed it. I also I have did some changes in js. It works properly now.

var GetMedicine= function () {
var url = UrlMedicine.GetMedicineChecked;

var isExp = $('#isExp').val();
        var url = url + '?isExp=' + isExp;
        $.get(url, null, function (data) {
            {
                var markup = "<option value='0'>Please Select Expird medicine</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("#medicineId").html(markup).show();
            }

        });
}

var GetMedicineList = function () {
    if ($('#isExp').is(":checked")) {
        userMedicineHistory.GetMedicine();
    }
}
return {
    GetMedicineList: GetMedicineList,
    GetMedicine: GetMedicine
};
$(document).ready(function () {
$("#isExp").change(userMedicineHistory.GetMedicineList)
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.