Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys

I am currently working on an autocomplete which uses a jsonp data type, issue i am having is when i am returning data from my controller it is returning the data in the following format

{"aaData":[{"SId":"934155","Name":"ABC CAMPAIGNS"},{"Sid":"926715","Name":"Inervation"},{"SId":"944847","Name":"International Technologies"}]}


I would like it to be returned as

callback([{"SId":"934155","Name":"ABC CAMPAIGNS"},{"Sid":"926715","Name":"Inervation"},{"SId":"944847","Name":"International Technologies"}]})



This is the model for my list in my controller
C#
 public class AutoCompleteModel
    {
        public string SId{ get; set; }
        public string Name{ get; set; }
        
    }


}


my web service call returns the list with the required data

i am just not sure on how to return the data in the format i require.

basically the code i am using

C#
Customer= new List<AutoCompleteModel>();



this is how my list is returned to the jsonp request


C#
return Json(
                 new
                 {

                     suppliers

                 },
                 JsonRequestBehavior.AllowGet);


What I have tried:

I have tried using newtonsoft serializer and deserializer and i am still getting the same result not sure what i am missing, or am i misunderstanding something?
Posted
Updated 7-Nov-16 8:07am
v2

1 solution

I would suggest that you look at [wikipedia JSONP]
Your sample code does not seem to support your issue
(could you supply more details about your controller? you seem to be switching between customer/supplier)
I have tried the following :
Newton soft serialiser:
C#
public class UnitTest1
   {
       [TestMethod]
       public void TestMethod1()
       {
           var Customer = new List<AutoCompleteModel>
                              {
                                  new AutoCompleteModel { Name = "test1", SId = "1" },
                                  new AutoCompleteModel { Name = "test2", SId = "2" },
                                  new AutoCompleteModel { Name = "test3", SId = "3" },
                              };
           var response = JsonConvert.SerializeObject(Customer);

       }
   }
   public class AutoCompleteModel
   {
       public string SId { get; set; }
       public string Name { get; set; }
   }

This produced a message in the following form - which I think you need:
[{"SId":"1","Name":"test1"},{"SId":"2","Name":"test2"},{"SId":"3","Name":"test3"}]

I have repeated the same for an MVC controller and a Web Api controller:
C#
public IHttpActionResult GetProducts(ODataQueryOptions<Product> queryOptions)
       {
           // validate the query.
           try
           {
               queryOptions.Validate(ValidationSettings);
           }
           catch (ODataException ex)
           {
               return this.BadRequest(ex.Message);
           }

           var settings = new ODataQuerySettings();
           var results = queryOptions.ApplyTo(this.context.AsQueryable(), settings) as IEnumerable<Product>;
           return this.Json(results);
       }

This also created the message in the same format (albeit a different model)
C#
[{"ID":1,"Description":"Orange","SalePrice":15.0,"ProductGroup":"Fruit"},{"ID":2,"Description":"Apple","SalePrice":5.0,"ProductGroup":"Fruit"},{"ID":3,"Description":"Pear","SalePrice":12.0,"ProductGroup":"Fruit"}]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900