ChrisP
ChrisP

Reputation: 10116

How do I return an empty string in a Null property when converted to JSON?

I have an ASP.NET MVC application that returns a Customer object that is converted to JSON using the Json() method.

return Json(Repository.Customer.Get(id));

One of the properties of the Customer object is Customer.Gender. If the Gender property contains a Null value, the JSON object received on the client contains

Gender: "<null>"

Is there a way to have the properties with Null values contain empty strings in the JSON object rather than the "null" text?

Upvotes: 1

Views: 1280

Answers (1)

Neil Thompson
Neil Thompson

Reputation: 6425

Modify your Customer object properties?

public class Customer
{
  public string GenderOrEmptyString {get {return this._gender ?? ""; }
}

Upvotes: 1

Related Questions