2011-11-15 32 views
1

我有一个ASP.NET MVC应用程序,该应用程序返回使用Json()方法转换为JSON的Customer对象。转换为JSON时,如何在Null属性中返回空字符串?

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

Customer对象的其中一个属性是Customer.Gender。如果性别属性包含Null值,在客户端上收到的JSON对象包含

Gender: "<null>" 

有没有办法有Null值的属性包含在JSON对象,而不是“空”文本空字符串?

+0

到这里看看: http://stackoverflow.com/questions/1031088/how-to-avoid-null-strings-when-binding-json-data-on-client-side –

回答

1

修改您的客户对象属性?

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

是的,它可以工作,但由于我几乎完全使用自动属性声明,所以进行这些更改会有很多工作。 – ChrisP

相关问题