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 7807439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:51:20+00:00 2026-06-02T02:51:20+00:00

I’m still learning Jquery and a bit confused with the task I have on

  • 0

I’m still learning Jquery and a bit confused with the task I have on my hands.

Seems like a simple task…
I have a box that I want to populate with Options on click. I don’t want it to be populated on page load, only when someone actually requests to see the list. Also it has to be populated only once. I really dont want the request to go out EVERY time someone expands the list.

I imagine that I’ll probably have a function call in my Select element

<select id="usersList" name="usersList" onclick="getUsers()">

And then have a Javascript getUsers() function make a call to my Json GetUsers() ActionMethod to get that list.
How ?

Something like…?

function getUsers()
{
 getJSON("/Users/GetUsers", null, function (data){}
}

or JQuery?…

$("usersList").click(
  $.getJSON("/Users/GetUsers", null, function (data) {}
  )

I should mention that I saw this post:
populate selectlist with json data in JQuery when the selectlist is loaded (not the document)

But I need help putting it all together please.
Thank you in advance!

  • 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-06-02T02:51:22+00:00Added an answer on June 2, 2026 at 2:51 am

    Here is the javascript I would use to populate the select box.

    $.getJSON("/Users/GetUsers", null, function(data) {
        $("#UsersList option").remove(); // Remove all <option> child tags.
        $.each(data.Users, function(index, item) { // Iterates through a collection
            $("#UsersList").append( // Append an object to the inside of the select box
                $("<option></option>") // Yes you can do this.
                    .text(item.Description)
                    .val(item.Id)
            );
        });
    });
    

    I would recommend that you trigger this on the page load and not when the user clicks the select box. jQuery AJAX is asynchronous by default (as it should be) so there will be a moment or two where the select box will be empty and this may greatly confuse the user.

    Was this the extent of your question or did you need assistance with the MVC side as well? If you’re using ASP.Net MVC there is actually a specific result type of JsonResult which makes creating JSON controller methods a bit simpler since they don’t require corresponding views. It also allows you to use anonymous data structures which makes method specific projections a bit simpler.


    EDIT Here is the basic method for returning a JSON structure the jQuery code above will understand. Note that the properties u.Username and u.ID depend on your Users object.

    [HttpPost] 
    public JsonResult GetUsers() { 
        return Json(new {
            Users = from u in _usersRepository.Get()
                    select new { Description = u.Username, ID = u.ID }
        }); 
    }
    

    I would recommend however that you abandon this approach as soon as possible and go with a standard JSON message structure. The one I use is something like this:

    {
        Success: bool,
        LoggedIn: bool,
        ClientErrors: [
            { Number: int, Description: string, Parameter: string },
            { Number: int, Description: string, Parameter: string }...
        ],
        ServerErrors: [
            { Id: int, Message: string },
            { Id: int, Message: string }...
        ],
        Data: {}
    }
    

    Here are a few reasons for doing this:

    1. Make no mistake, your JSON method library is a protocol of sorts and as such it benefits from a standard header.
    2. Error handling benefits the most from this. Having a consistent method for letting the user know something went wrong is invaluable down the road.
    3. Differentiating between client errors (bad username, incorrect password, etc) and server errors (database offline) is very important. One the user can fix, the other they cannot.
    4. Client Errors should return not only a message but the parameter in the method which cause it (if it applies) so that you can easily display these error messages next to the appropriate input fields.
    5. Client Errors should also provide a unique number (per method) which identifies the error. This allows for multilingual support.
    6. Server errors should provide the Id of the error which was logged. This way you can allow the user to contact support and reference the particular error they are running into.
    7. The loggedin boolean allows any page which uses methods that require authentication to easily redirect the user back to the login screen if necessary since a redirect action result of a JSON method will have no effect.
    8. Finally the Data field is where the method specific data should be provided.

    By no means should you consider my structure as the way to do it. Clearly your application needs may differ greatly from mine. Consistency, more than any particular structure is the lesson here.

    EDIT: 06/01/2016 – Seeing as people are still looking at this answer. With regard to populating the drop down I would recommend looking into using a two way binding framework. Personally I prefer Knockout.JS for how easy and lightweight it is although Angular.JS would do the job as well. Aside from that the rest of this answer is still fairly up to date.

    EDIT: 8/10/2016 – Code was missing a comma.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
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
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.