Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6341125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:58:07+00:00 2026-05-24T19:58:07+00:00

I’m trying to populate a second drop down list (which contains counties) depending on

  • 0

I’m trying to populate a second drop down list (which contains counties) depending on what is selected on the first drop down list (which contains countries).

I have a controller action called GetCounties:

  public JsonResult GetCounties(string id)
    {
        DBEntities dc = new DBEntities();
        JsonResult result = new JsonResult();
        var filteredDivisions = from div in dc.Divisions
                                where div.CountryID == 1 //id
                                select div;
        result.Data = filteredDivisions.ToList();        
        return Json(result.Data, JsonRequestBehavior.AllowGet);          

        //         return Json(new[] {
        //    new { Id = filteredDivisions.ToArray()[0].DivisionID, Value = filteredDivisions.ToArray()[0].DivisionName },
        //    new { Id = 2, Value = "value 2" },
        //    new { Id = 3, Value = "value 3" },
        //}, JsonRequestBehavior.AllowGet);
    }

Notice the section that is commented out. That works fine for me, (I get the first value from the divisions list, and the other two called “value 2” and “value 3”. I used that there to test the callback, but it was the only thing that gave me results. Using:

 return Json(result.Data, JsonRequestBehavior.AllowGet);     

doesn’t achieve anything on the view side. When debugging, all the right values are there, but they don’t come up on the view side’s drop down list. As I said, the only results I get is by using the code that is commented out (the hardcoded values, which of course are not good enough). I tried to use for loops to iterate through the list in order to populate the array, but nothing works, I get errors like anonymous type is read only so I cannot set it again.

This is my jquery code:

  $(function () {
            $.getJSON('/Entity/GetCounties', function (result) {
                var ddl = $('#DivisionsList');
                ddl.empty();
                $(result).each(function () {
                    $(document.createElement('option'))
            .attr('value', this.Id)
            .text(this.Value)
            .appendTo(ddl);
                });
            });
        });

The divisionslist is of course the dropdown with all the counties, which by using the non-commented return statement above, just returns unchanged, i.e. containing ALL of its values, and not just the ones that should come up depending on the selected country.
so please HOW CAN I POPULATE AN ARRAY WITH THE FILTERED RESULTS SO THAT I CAN RETURN IT TO THE VIEW USING JSON? I want to use THIS code that I have posted, as it was THE ONLY ONE that ever worked. I just need to swap the hardcoded values with the right ones. that’s where i’m having trouble.

please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:58:08+00:00Added an answer on May 24, 2026 at 7:58 pm

    I solved it myself. What i did is this:

       public JsonResult GetDivisions(int id)
        {
            ASNEntities dc = new ASNEntities();
            JsonResult result = new JsonResult();
            var filteredDivisions = from div in dc.Divisions
                                    where div.CountryID == id
                                    select div;
    
            List<object> listToReturn = new List<object>();
    
            for (int i = 0; i < filteredDivisions.Count(); i++)
            {
                Object[] obj = new[]{
                 new { Id = filteredDivisions.ToArray()[i].DivisionID, Value = filteredDivisions.ToArray()[i].DivisionName },
            };
    
               listToReturn.Add(obj[0]);              
            }
    
            return Json(listToReturn.ToArray(), JsonRequestBehavior.AllowGet);
    
            //return Json(new[] {
            //    new { Id = filteredDivisions.ToArray()[0].DivisionID, Value = filteredDivisions.ToArray()[0].DivisionName },
            //    new { Id = 2, Value = "value 2" },
            //    new { Id = 3, Value = "value 3" },
            //}, JsonRequestBehavior.AllowGet);
    
        }
    

    and it works great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.