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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:24:20+00:00 2026-05-23T13:24:20+00:00

So I need to pass to a JavaScript function an array of strings in

  • 0

So I need to pass to a JavaScript function an array of strings in my view based on data from the database. So I have this code in the controller:

        string top_six_string = "[";
        foreach (ObjectModel om in collection)
        {
            myProject.Models.BlobFile file = null;
            if (om.BlobFile != null)
            {
                file = om.BlobFile;
            }
            else if (om.BlobFiles.Count != 0) 
            {
                file = om.BlobFiles.First();
            }
            if (file != null)
            {
                top_six_string += " \"" + file.BlobFileID + "\",";
            }
        }
        top_six_string = top_six_string.TrimEnd(',');
        top_six_string += "]";
        ViewBag.TopSixList = top_six_string;

Now, I don’t particularly understand why we have both a BlobFile field and a BlobFiles collection, but that’s not that point. The point is, debugging shows that I accurately the get the string I want (of the form [“25”, “21”, “61”, “59”]).

But when running the JavaScript, I got the confusing error “Unexpected character &”, and a little source-viewing in Chrome led me to learn that the string came out looking like this:

[ "25", "21", "61", "59"]

So my assumption is that the ViewBag is sanitizing string that it is passed for display in HTML, but obviously that isn’t my concern right now. Am I correct in my assumption? Is there another way to pass the view this information? Is there a way I can coerce the string back to quotes afterwards?

  • 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-23T13:24:21+00:00Added an answer on May 23, 2026 at 1:24 pm

    The problem is most likely when you output the contents of the ViewBag in your View. By default the Html helpers sanitize output to help protect against injection attacks.

    What you want is this when outputting the value in your View: @Html.Raw(ViewBag.TopSixList)

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

Sidebar

Related Questions

I need to pass an array from JavaScript to a page method in C#.
I have a user control in a repeater that I need to pass data
Possible Duplicate: How to pass text in a textbox to javascript function? I need
I have a java application I need to pass some info to a C++
I'm creating a function where I need to pass an object so that it
I have a .NET program and a Borland Win32 program that need to pass
I have a dict, which I need to pass key/values as keyword arguments.. For
I need to send a JavaScript array via querystring to my .ashx. In my
I need some help pulling cell data from table when a user clicks the
I need to pass an ID and a password to a batch file at

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.