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 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 a page http://www.mysite.com/image.aspx , that I want to load and display an
I have a <ul> that is populated with javascript after the initial page load.
I have a page that uses jQuery to load an option dropdown on page
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 page that is taking a couple of seconds to load due
I have to do a asp.net web page that when load will say something
I have a large dataset that load in with a fill method on page
I have a form and it contains dropdown box and different div items. Based

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.