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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:51:53+00:00 2026-05-11T10:51:53+00:00

There may be a simpler way of doing this and I am all ears

  • 0

There may be a simpler way of doing this and I am all ears if there is. My situation is I have a dropdownlist on a form which I successfully populate with text and values. I also need to have additional related string values from the same table row in the db table available on the client so when the user selects from the dropdown this related data gets populated in a textbox on the form. There are only 4 records I’m dealing with so storing on the client is no big deal. I thought about passing this data via ViewData as a list and loading into a javascript array. When the user selects from the dropdown – I would determine the selected index and get the related data I need from the array. I am already using the value of the dropdown item for other required data so I need a way to get this related data without making a return trip to the server. If I am on the right track could someone post a simple example of populating a js array with sting values returned as a List in the ViewData.

Thanks,

Mike

  • 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. 2026-05-11T10:51:54+00:00Added an answer on May 11, 2026 at 10:51 am
    var myArray = [ <% foreach (string item in ViewData['list'] as List<string>) { %> '<%= item %>', <% } %> ]; 

    Having a comma at the end will reportedly break in IE, so I would suggest a view extension helper method to make the code easier to manage:

    <%= Html.JavaScriptArray(ViewData['list'] as List<string>, 'myArray') %> 

    Put this helper method somewhere in you solution:

    public static string JavaScriptArray(this HtmlHelper htmlHelper, IList<string> values, string varName) {     StringBuilder sb = new StringBuilder('var ');     sb.append(varName);     sb.append(' = [');     for (int i = 0; i < values.Count; i++) {         sb.append(''');         sb.append(values[i]);         sb.append(''');         sb.append(i == values.Count - 1 ? '\n' : ',\n'); // Not the prettiest but it works...     }     sb.append('];');     return sb.toString(); } 

    Technically the extension method can go anywhere, you’ll just need to include the namespace in you .aspx file. Practically its best to keep them in logically separated classes, such as MyApp.Mvc.Extensions.JavaScriptExtensions, MyApp.Mvc.Extensions.LinkExtensions

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

Sidebar

Related Questions

This may not be possible for the carousel but is there a way to
[Preamble: Whereas I realize there may be simpler ways to do this (i.e., just
This may be the really wrong way of doing this and so if it
I have the following String Sample however in some occurences there may be one
May I know if there is any way to remove non-alphabetical symbols from a
I may get some heat for this question because there are a lot out
This may fall under you can't, and there's no reason to anyway, but I'm
Is there an easy way to recursively ZIP a directory that may or may
There is any ways to listing all main directories present in php server(may it
--EDIT-- I believe this is a valid question that may have multiple answers (as

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.