1

In my Get method of my REST service,

public HttpResponseMessage Get()
 {
  CustomerRepository lr = new CustomerRepository ();
  IQueryable<Customer> data = lr.GetCustomer();

  var resp = new HttpResponseMessage(HttpStatusCode.OK);
  resp.Content = new ObjectContent<IQueryable<Customer>>(data, new JsonMediaTypeFormatter());        
  return resp;
 }

the following test example would be returned to the view as a json string:

{ id: '1', name:'California', type:'Student', parent:''}, //parent row
{ id: '2', name:'Harry', type:'Student', parent: '1' },
{ id: '3', name:'James', type:'Student', parent: '1' },
{ id: '4', name:'Sally', type:'Student', parent: '1'},

how do I remove the parent row's parent field to be like so before I send it back to UI:

{ id: '1', name:'California', type:'Student'}, //parent row
{ id: '2', name:'Harry', type:'Student', parent: '1' },
{ id: '3', name:'James', type:'Student', parent: '1' },
{ id: '4', name:'Sally', type:'Student', parent: '1'},

Thank you!

EDIT:

I found a solution. In my customer class, I just decorate the field.

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
 public string parent { get; set; }

2 Answers 2

0

I had this problem and another one at the same time, I needed to join two tables before sending them via the API..

What I did was create another class that contained only the fields I needed and made a constructor that accepts the first object.

So in this case I would make a "ParentFormat" class with id,name,type, and the constructor of ParentFormat receives a Parent object... then you make another Get method in the API that returns the ParentFormat. There might be a better solution out there but this worked fine for me so I hope it can help you out ;)

EDIT : Actually I knew something bout anonymous types but it was too for in my head, after a 2second search I found this : http://msdn.microsoft.com/en-us/library/bb397696.aspx

Using anonymous types seems like a good way and im definitly going to try it

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

3 Comments

I saw another one asking something similar but they used data annotation EmitDefaultValue. But I wasn't able to decorate my classes with DataContract and my properties with DataMember to use EmitDefaultValue: stackoverflow.com/questions/10150312/…
Thanks for replying, found something even easier: [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string parent { get; set; }
Thanks for sharing... add it as an answer and accept it ;)
0

In general, when you do not have a very simple application, it is a better idea to use separate dto-classes/viewmodels (data-transfer-objects). In most cases you do not want to expose all data, e.g. the identity or sensitive data. You also dont want your api to be coupled to the business logic and/or the data model.

You can read more about this here: Does it Make Sense to have ViewModels in the Webapi?

Google also has a lot of blog posts about this topic.

Comments

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.