我有一个返回 json 结果的操作,但一些属性为空,我想将它们转换为空字符串。我听说我可以使用 DefaultValue("")
,但它仍然返回 null
而不是空字符串。
Action 是:
[HttpGet]
public ActionResult GetResults(string date)
{
var data= GetData(); // returns List<Foo>
var json = Json(data, JsonRequestBehavior.AllowGet);
return json;
}
Foo
类是:
public class Foo
{
public string Bar1;
[DefaultValue("")]
public int? Bar2;
}
最佳答案
您不能将 nullable int
设置为 ""
的默认值
尝试
[DefaultValue(0)]
public int? Bar2;
关于c# - 返回 json 结果并将 null 转换为空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15308726/