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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:33:06+00:00 2026-05-14T04:33:06+00:00

I’m returning JSON from my controller to my view in order to populate a

  • 0

I’m returning JSON from my controller to my view in order to populate a jquery autocomplete textbox in MVC. The problem is, some of my data contains commas, and therefore is split by the autocomplete helper.

Heres my code.

Controller:

    public ActionResult GetData()
    {
        var data = repository.GetData();

        return Json(data);
    }

View (script):

    $.post("../MyController/GetData",

        function(data) {
        var evalData = eval(data) + ""; //formats the text

        $("#Data").autocomplete(evalData.split(","),
        {
            max: 500,
            matchContains: true
        });
    });

As you can see, I am using the jquery .split helper to split the returned Json. Should I be using regex or should I go with a completely different approach?

  • 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-14T04:33:07+00:00Added an answer on May 14, 2026 at 4:33 am

    I assume you’re using the Autocomplete built into jQuery UI 1.8. If you are, you have a couple of different options.

    1. Based on one of the samples that’s available in documentation, you can just give it a string specifying the URL of your service and not have to worry about parsing the return yourself. So something along the lines of:

      $("#Data").autocomplete({
          source: "../MyController/GetData"
      });
      

      Your action will most likely need to respond to get requests as well as post though and your data may need to be in the form of [{ label: "something", value: "1" }, ... ]
      Which you could do by shaping your data using a Linq query before sending it out.

      var data = from d in repository.GetData()
                 select new
                 {
                     label = d.[whatever you want the label to be]
                     value = d.[whatever you want the value to be]
                 };
      return Json(data);
      
    2. You can combine your current implementation with parts of the example above to and get something like this:

      $.post("../MyController/GetData",
          function(data) {
              $("#Data").autocomplete({
                  source: JSON.parse(data),
                  max: 500,
                  matchContains: true
              });
          });
      

      This assumes that data is in the form of [{ label: "something", value: "1" }, ... ] (see 1 for how to shape it using Linq). The JSON parser will take care of commas in quotes problem for you.

    3. You can also specify a method to call when you want to retrieve data.

      $("#Data").autocomplete({
          source: function(request, response) {
              $.ajax({
                  url: "../MyController/GetData",
                  dataType: "json",
                  success: function(data) {
                      response( data );
                  }
              })
          }
      });
      

      (see above about how to shape data using Linq)

    A couple of comments about your current implementation.

    • You should considering using the UrlHelper instead of hard-codeing the URL in case you ever change your routes.

    • Instead of eval you should use the JSON2.js library to parse the return value from your action. It is generally a little bit more secure and yields better performance in newer browsers that support native JSON parsing.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.