1

i need help to get data from web Api. I use EF with two tables named "Reichweites" and "Points"(child)

This is my DTO class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace xms_ef_vers1.Models
{
    public class ReichweitePointsDto
    {

        public ReichweitePointsDto() { }

        public ReichweitePointsDto(Reichweite reichweite)
        {
            ReichweiteID = reichweite.Id;
            Name = reichweite.Name;
            Potenzial = reichweite.Potenzial;
            Geschlecht = reichweite.Geschlecht;
            CPGRP = reichweite.CpGRP;
            ZielGRP = reichweite.ZielGRP;
            Benutzer = reichweite.Benutzer;

            PointListe = new List<PointListDto>();
            foreach (Points item in reichweite.PointListe)
            {
                PointListe.Add(new PointListDto(item));
            }
        }

        [Key]
        public int ReichweiteID { get; set; }
        public string Name { get; set; }
        //[Required]
        public int Potenzial { get; set; }
        //[Required]
        public string Geschlecht { get; set; }
        //[Required]
        public int CPGRP { get; set; }
        public int ZielGRP { get; set; }
        public string Benutzer { get; set; }

        public virtual List<PointListDto> PointListe { get; set; }

        public Reichweite ToEntity()
        {
            Reichweite reich = new Reichweite
            {
                Id = ReichweiteID,
                Name = Name,
                Potenzial = Potenzial,
                Geschlecht = Geschlecht,
                CpGRP = CPGRP,
                ZielGRP = ZielGRP,
                Benutzer = Benutzer,
                PointListe = new List<Points>()
            };

            foreach (PointListDto item in PointListe)
            {
                reich.PointListe.Add(item.ToEntity());
            }

            return reich;
        }

    }
}

And my Data Model:

1:n relation

Main Table is Reichweites

  • Id
  • Name
  • Potenzial
  • Geschlecht
  • CpGRP
  • ZielGRP
  • Benutzer

Child Table is Points

  • Id
  • GRP
  • RW_TV
  • Cost_TV
  • Rw_ZuWGS
  • Cost_I_Rea
  • ReichweiteId fk

I want provide the data in this format:

[   
  {
    "User":"testuser",
    "CPGRP":21321321,   
    "Sex":"men",    
    "Name": "test",
    "Potenzial":213213,
    "ReichweiteID":0,   
    "ZielGRP": 21321321 
    "PointList":[
                      {
                       "Cost_I_Rea":"22202771.01",
                       "Cost_TV":"213213210.0" ,
                       "GRP":10,   
                       "ID":0, 
                       "ReichweiteID_F":1, 
                       "RW_TV":"9.603",    
                       "RW_Zuwgs":"9.603",
                      },
                      {
                        "Cost_I_Rea":"22202771.01",
                        "Cost_TV":"213213210.0" ,
                        "GRP":10,   
                        "ID":0, 
                        "ReichweiteID_F":1, 
                        "RW_TV":"9.61103",  
                        "RW_Zuwgs":"9.6043",
                      },
                      {
                        "Cost_I_Rea":"22202771.01",
                        "Cost_TV":"213213210.0" ,
                        "GRP":10,   
                        "ID":0, 
                        "ReichweiteID_F":1, 
                        "RW_TV":"9.61103",  
                        "RW_Zuwgs":"9.6043",
                      },
                      {
                        "Cost_I_Rea":"22202771.01",
                        "Cost_TV":"213213210.0" ,
                        "GRP":10,   
                        "ID":0, 
                        "ReichweiteID_F":1, 
                        "RW_TV":"9.61103",  
                        "RW_Zuwgs":"9.6043",
                      }
              ]
   }
]

some additional requirement is that i must fiter by id and user name.

This is how i try it now but it do not work.

What do you need to understand my question better?

[ResponseType(typeof(Reichweite1Dto))]

public async Task<IHttpActionResult> GetReichweite(int id)
{
    var reich = await db.Points.Include(b => b.ReichweiteId)
        .Select(b => 
        new ReichweitePointsDto ()
        {
            Benutzer = b.Reichweite.Benutzer,
            Name = b.Reichweite.Name,
            Geschlecht = b.Reichweite.Geschlecht,
            CPGRP = b.Reichweite.CpGRP,
            Potenzial = b.Reichweite.Potenzial,
            ZielGRP = b.Reichweite.ZielGRP,
            PointListe = (from item in reich
                          select new Points()
                              {
                                  GRP = item.GRP,
                                  RW_TV = item.RW_TV,
                                  Cost_TV = item.Cost_TV,
                                  Rw_ZuWGS = item.RW_Zuwgs,
                                  Cost_I_Rea = item.Cost_I_Rea,
                              }).ToList()
          };
      return reich;
}
9
  • I don't understand the question , what exactly are you have trouble with? once you have filled the models with data all you have to do is in the controller this.Json(reich); and it will turn c# objects into JSON like that for you Commented Oct 27, 2014 at 13:29
  • I don´t know how i have to write the get method that i get the data like the json example Commented Oct 27, 2014 at 13:34
  • What I meant was , are you already properly returning all the data in c# objects? and just need it converted to JSON? Commented Oct 27, 2014 at 13:58
  • Yes only get the Data and convert it in to an json object. Commented Oct 27, 2014 at 14:09
  • so , you are already getting the data just fine?? and are having problems converting into JSON? Commented Oct 27, 2014 at 14:09

2 Answers 2

1

Yes i have it. The key was to keep it simple. Many thanks to Scott.

Now my method works fine and provides the data in the Json format which i wanted!

Here the Methods:

    namespace xms_ef_vers1.Controllers
    {
        public class TVKurveController : ApiController
        {
            private ReichweitenKurveContext db = new ReichweitenKurveContext();


            // GET api/TVKurve/5
            public IHttpActionResult GetReichweite(int id)
            {
                var obj = db.Reichweites.Where(r => r.Id == id && r.Benutzer == user.identity.name);
                return Ok(obj);
            }

            /////////////////////
            // GET api/TVKurve
            [ResponseType(typeof(ReichweitePointsDto))]

            public IEnumerable<Reichweite> GetReichweite()
            {
                var obj = db.Reichweites.Take(100);
                return obj;
            }
         }
   }

Someone was missing in the WebApiConfig.cs:

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;

config.Formatters.Remove(config.Formatters.XmlFormatter);

And here how the result will be:

[
    {
        "$id": "1",
        "PointListe": [
            {
                "$id": "2",
                "Reichweite": {
                    "$ref": "1"
                },
                "Id": 2771,
                "GRP": 10,
                "RW_TV": 1000,
                "Cost_TV": 15600,
                "Rw_ZuWGS": 1000,
                "Cost_I_Rea": 15.6,
                "ReichweiteId": 21
            },
            {
                "$id": "3",
                "Reichweite": {
                    "$ref": "1"
                },
                "Id": 2772,
                "GRP": 20,
                "RW_TV": 2000,
                "Cost_TV": 31200,
                "Rw_ZuWGS": 1000,
                "Cost_I_Rea": 15.6,
                "ReichweiteId": 21
            },
            {
                "$id": "4",
                "Reichweite": {
                    "$ref": "1"
                },
                "Id": 2773,
                "GRP": 30,
                "RW_TV": 3500,
                "Cost_TV": 46800,
                "Rw_ZuWGS": 1500,
                "Cost_I_Rea": 10.4,
                "ReichweiteId": 21
            },
            {
                "$id": "5",
                "Reichweite": {
                    "$ref": "1"
                },
                "Id": 2774,
                "GRP": 40,
                "RW_TV": 4500,
                "Cost_TV": 62400,
                "Rw_ZuWGS": 1000,
                "Cost_I_Rea": 15.6,
                "ReichweiteId": 21
            }
        ],
        "Id": 21,
        "Name": "Short Curve",
        "Potenzial": 132132132,
        "Geschlecht": "men",
        "CpGRP": 1560,
        "ZielGRP": 1560,
        "Benutzer": "admin"
    }
]

Maybe you can tell me what the "$id" and "$ref" is.

Best and thank you very much

Sign up to request clarification or add additional context in comments.

1 Comment

converting the German to english helps a lot too before posting
0

If I understood correctly form your comments you say that you are already getting all the data just fine - you just need it converted into JOSN

simple just change the last line of the controller action form :

return reich;

to:

return this.Json(reich);

MVC and WebApi have built in JSON serializers

try this:

public IHttpActionResult GetReichweite(int id)
        {
            var obj = db.Points.Take(100);                
            return Ok(obj);
        }

that should simpify things abit

5 Comments

Do not work. Something went wrong in the method. (from item in reich select new Points() reich is red underlined "missing deklaration"
yea - your whole query doesn't look like it could possibly work the way you have it written , I wasn't sure about that . You did say you were successfully getting data back - and asked how to convert that data into JSON - this answer here is how to do that
Sorry my fail. Can you help me to get the data which i need.
updated answer - take out async until you get it working normally
Ok i´ve try it but get an Self referencing loop detected for property error back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.