locked
Response with more then wrapper RRS feed

  • Question

  • User-193806752 posted

    Hi, if i have selected one field from db i have done this:

    var carData= (from db in contestoDB.Cars
    where db.ID == idCar
    select db).Single();
    
    var wrapper = new
    {
    
    name= carData.Name,
    color= carData.Color,
    make = carData.Make
    
    };
    
    context.Response.Write(JsonConvert.SerializeObject(wrapper)); 

    Ok. It's working for one field!

    But if i have more than one field how do i do it? Wrapper array? How can i make a wrapper array to response for ajax calling?

    Sorry for my bad english....you know i'm italian...

    Please, help me thanks

    Cris

    Tuesday, October 27, 2015 12:40 PM

Answers

  • User281315223 posted

    Well currently, you are pulling just a single car based on it's ID from your Context. You could change this slightly and pull all of your cars by removing the Single() call and using something like the following :

    // Grab your cars
    var carData = contestoDB.Cars.ToList();
    
    // Map them to your specific objects (specifically an array)
    var wrappers = carData.Select(c => new { name = c.Name, color = c.Color, make = c.Make })
                          .ToArray();
    
    context.Response.Write(JsonConvert.SerializeObject(wrapper)); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 27, 2015 4:19 PM

All replies

  • User281315223 posted

    Well currently, you are pulling just a single car based on it's ID from your Context. You could change this slightly and pull all of your cars by removing the Single() call and using something like the following :

    // Grab your cars
    var carData = contestoDB.Cars.ToList();
    
    // Map them to your specific objects (specifically an array)
    var wrappers = carData.Select(c => new { name = c.Name, color = c.Color, make = c.Make })
                          .ToArray();
    
    context.Response.Write(JsonConvert.SerializeObject(wrapper)); 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 27, 2015 4:19 PM
  • User-193806752 posted

    Well currently, you are pulling just a single car based on it's ID from your Context. You could change this slightly and pull all of your cars by removing the Single() call and using something like the following :

    // Grab your cars
    var carData = contestoDB.Cars.ToList();
    
    // Map them to your specific objects (specifically an array)
    var wrappers = carData.Select(c => new { name = c.Name, color = c.Color, make = c.Make })
                          .ToArray();
    
    context.Response.Write(JsonConvert.SerializeObject(wrapper)); 

    Perfect answer and working code!!!

    Thanks!

    Wednesday, October 28, 2015 3:59 AM