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

  • Home
  • SEARCH
  • 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 7073787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:58:18+00:00 2026-05-28T05:58:18+00:00

I have a page that I load a select dropdown box but I want

  • 0

I have a page that I load a select dropdown box but I want to have javascript code toggle this list to a different set of choices.

I have the code that clears and repopulates a select list box in javascript

function UpdateDropdown(list, dropdownSelector) {
var options = '';
$.each(list, function (index, value) {
    if (value.Text == null) {
        value.Text = '';
    }
    if (value.Value == null) {
        value.Value = '0';
    }

    options += '<option value="' + value.Value + '">' + value.Text + '</option>';
});
$(dropdownSelector).html(options);

}

so that is not the issue but my question is How can i store an array locally from my view Model to lookup later from javascript? Should i store it in a hidden select box? is there a recommended practice here?

  • 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-28T05:58:18+00:00Added an answer on May 28, 2026 at 5:58 am

    The easiest and cleanest solution is to just write some json to your view, stored in a variable that you then access inside your UpdateDropdown function. In your view:

    <script type="text/javascript">
      var globalData = @Html.Raw(Json.Encode(Model.MyArray));
    </script>
    

    The exact method you use to output json will differ depending on the version of ASP.NET MVC you’re using, but that should put you on the right track.

    And general javascript best practice is to create a single namespace for your code that you then put all your other variables into, so that you don’t pollute the global namespace. So really it should be something like:

    <script type="text/javascript">
      var MyApp = {};
      MyApp.dropdownData = @Html.Raw(Json.Encode(Model.MyArray));
    </script>
    

    But the first block of code I provided would be fine to get started with.

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

Sidebar

Related Questions

I have this code in the page load...For some reason the dropdown binds to
I have written some javascript code that's supposed to put options into a select
I have a drop down list's select tag that looks like this: <select name=MyName
I have a dropdown list boxes in the page. When I Load the page.
I have a form and it contains dropdown box and different div items. Based
I have an ASP.net mvc page that executes a jquery script on load. The
I have a page. In that page's header i have all of my javascript.
I have a page that is taking 37 seconds to load. While it is
I have a page model that I want to find by a handle that
I have a page with a ScriptManager, a generic HTML drop-down list ( <select>

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.