2

I cannot get this to work no matter how much searching and different variations I keep trying:

I am trying to dynamically populate a select list from my jquery:

var url = '@Url.Action("GetCounties", "Account")' + '/' + $("#State").val();
            $.get(url, function (data) {
                $('#county').empty();
                $.each(result, function (index, val) {
                    $('#county')
                    .append($("<option></option>")
                    .attr("value", val.Text)
                    .text(val.Text));
                });
            });

Controller Action using Entity Framework:

public JsonResult GetCounties(string id)
{
    return Json(GetCountySelectList(id), JsonRequestBehavior.AllowGet);
}

private SelectList GetCountySelectList(string id)
{
    var counties = db.ZipCodeDataBase.Where(x => x.State.Contains(id)).OrderBy(x => x.County).Select(x => x.County).Distinct().ToList();
    SelectList list = new SelectList(counties);
    return list;
}

Every variation of the generated select list will not return the data as it is required by the jquery.

Any assistance would be GREATLY appreciated!

1
  • what's result? did you mean data.d? Commented May 4, 2012 at 19:45

1 Answer 1

3

You are calling your return variable data and then referencing it as result in the $.each. Try fixing that first.

Sign up to request clarification or add additional context in comments.

3 Comments

That was it! Thank you! I guess I was just looking at it too much today.
You may wish to delete the question as it isn't a real question anymore.
No need to delete as it helped me! :) I just saw that I could actually return SelectListItem items using Json. That's great.

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.