0

Below is my DTO.

public class CustomerTO
{
  public int Id { get; set;}

  public string Name { get; set;}

  //& so on
}

But when I returning the JSON string from my action, I want only few properties to be send to the client.

My Json should look like

{
   "id": 1,
   "name": "Ram"
}

My Action.

public string GetCustomers()
{
  List<CustomerTO> customers = dal.Get();
  var strJson = JsonConvert.SerializeObject(customers);
  return strJson;
}

Above action returns string as:-

[
{
  "id":1,
  "name":"Ram",
  "age":27,
  "Country":"India"
},
{
  "id":2,
  "name":"Shyam",
  "age":27,
  "Country":"India"
}
]

How do I filter the properties in JSON from DTO??

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Possible duplicate of [How to exclude property from Json Serialization](http://stackoverflow.com/questions/10169648/how-to-exclude-property-from-json-serialization) – Nate Barbettini Apr 03 '17 at 14:37
  • @NateBarbettini, I checked using [ScriptIgnore] mentioned in the link but its not filtering – Kgn-web Apr 03 '17 at 14:45
  • Use `[JsonIgnore]`: http://stackoverflow.com/a/25566387/3191599 (Not sure why that's not the accepted answer on the older question) – Nate Barbettini Apr 03 '17 at 14:46
  • @NateBarbettini, May I know which namespace is required for the [JsonIgnore] – Kgn-web Apr 03 '17 at 14:46

1 Answers1

1

Decorate the members you don't want serialized to the client in your DTO with [JsonIgnore]

brad
  • 529
  • 4
  • 5