1

I have this code in my view :

<script>
    var isChanged = false;
    $(function () {
        $('#stageOne').change(function () {
                $.ajax({
                    url: "/Shop/ChangeStageTwo/",
                    data: { item: $("#stageOne option:selected").text() },
                    type: 'post',
                    success: function (myJSONdata) {
                        $("#stageTwo").html("");      
                        var items = myJSONdata;
                        for (var i = 0; i < items.length; i++) {
                            var item = items[i];
                            var optionhtml = '<option value="' + item.text + '">' + items[i] + '</option>';
                            $("#stageTwo").append(optionhtml);
                        }
                 }
            });

        });
    });
</script>

And this is my ChangeStageTwo function :

    public JsonResult ChangeStageTwo(string item)
    {
        // Do something...          

        var query = from f in db.Stages
                    where f.Code.Contains(tempDelivCod) && f.Code.Length > 4
                    select f.Name;
        var sItems = new SelectList(query);
        return Json(sItems, JsonRequestBehavior.AllowGet); 
    }

But at run time I get [object Object] in my DropDownList, not the exact value. what is the problem?

2
  • 1
    Could you add a typical JSON example please? Commented Apr 6, 2013 at 13:44
  • Json data is a string list like this "aa","bb",... now iget undefined error! Commented Apr 6, 2013 at 19:02

2 Answers 2

1

I think this function will works :

var items = myJSONdata;//somethink like ["aa", "bb"]...
$(items).each(function(index, optionText) {
    $("#stageTwo").append($('<option />').attr('value', optionText).text(optionText));
});
Sign up to request clarification or add additional context in comments.

Comments

0

This line:

var optionhtml = '<option value="' + item.text + '">' + items[i] + '</option>';

should be something like:

var optionhtml = '<option value="' + item.text + '">' + item.text + '</option>';

or

var optionhtml = '<option value="' + item.id + '">' + item.text + '</option>';

If you provide your JSON output, I can be more specific.

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.