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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:51:52+00:00 2026-05-26T11:51:52+00:00

I have the following code in my controller: public ActionResult MyPage() { var vm

  • 0

I have the following code in my controller:

    public ActionResult MyPage()
    {
      var vm = new MyViewModel();
      vm.StringList = "'" + String.Join("','", MyModel.GetList().Select(r => r.Name).ToArray()) + "'";

        return View(vm);
    }

and in my asp.net-mvc view i have the following code:

 var myJavascriptArray = [<%=Model.StringList %>];

inside a javascript function

this works fine until one of the names in the:

 MyModel.GetList()

call has a apostrophe in it like “Bill O’Brien”

as that messes up my javascript code as it thinks the ‘ after the O is the end of that entry and the line breaks.

What is the best way to escape out of this character so i can have an array in javascript like that will ultimately show up as this:

var myJavascriptArray = [”Item 1′,Item 2′,’Item 3′,’Item 4′];

and deal with ‘Bill O’Brien’ being one of the entries.

  • 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-26T11:51:53+00:00Added an answer on May 26, 2026 at 11:51 am

    Are you trying to do some sort of JSON serialization with all those manual apostrophes and stuff? Don’t.

    Simply define a view model that will contain a list of names:

    public class MyViewModel
    {
        public string[] Names { get; set; }
    } 
    

    that you will populate in your controller and pass to the view:

    public ActionResult MyPage()
    {
        var model = new MyViewModel
        {
            Names = MyModel.GetList().Select(r => r.Name).ToArray()
        }
        return View(model);
    }
    

    and in the view JSON serialize this property:

    var myJavascriptArray = <%= new JavaScriptSerializer().Serialize(Model.Names) %>;
    

    Or if you are using ASP.NET MVC 3:

    var myJavascriptArray = <%= Json.Encode(Model.Names) %>;
    

    which will result in a correctly JSON encoded array of strings in the view:

    var myJavascriptArray = ["name 1", "name 2", "name 3"];
    

    which you can manipulate as a normal js array:

    alert(myJavascriptArray[1]);
    

    Obviously this technique works with more complex object graphs than simple array of strings. You could safe serialize entire object models without worrying about single, double quotes, parenthesis, etc … Of course in addition to all this the javascript serializer will deal with names like Bill O'Brien and properly encode them.

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

Sidebar

Related Questions

I have the following code in my controller: public ActionResult Details(int id) { var
Inside my controller's action I have the following code: public ActionResult GridAction(string id) {
I have the following code in my controller called UserController : public ActionResult Details(string
In my controller I have the following (code omitted for brevity): public ActionResult Edit(string
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
i'm new to routing in aspnet mvc.. i have following code: Action Controller public
I have the following code in controller: public ActionResult SearchPage() { return View(); }
I have the following code in my Simulacion controller: [Authorize] public ActionResult Create() {
I have the following code: [HttpGet] public ActionResult Edit(int req) { var viewModel =

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.