C# 避免在JSON中为AJAX调用返回空值
我有以下方法:C# 避免在JSON中为AJAX调用返回空值,c#,jquery,ajax,model-view-controller,C#,Jquery,Ajax,Model View Controller,我有以下方法: [HttpPost] [AjaxOnly] public JsonResult ValidateInput(string text) { return new EmptyJsonResult(); } /// <summary> /// returns a JSON result that is marked as being empty. /// </summary> public sealed class EmptyJsonResult :
[HttpPost]
[AjaxOnly]
public JsonResult ValidateInput(string text)
{
return new EmptyJsonResult();
}
/// <summary>
/// returns a JSON result that is marked as being empty.
/// </summary>
public sealed class EmptyJsonResult : JsonResult
{
public EmptyJsonResult()
{
Data = new JsonResultData
{
Empty = true
};
}
}
public class JsonResultData
{
public bool Empty { get; set; }
public string[] Errors { get; set; }
}
[HttpPost]
[仅限AjaxOnly]
公共JsonResult ValidateInput(字符串文本)
{
返回新的EmptyJsonResult();
}
///
///返回标记为空的JSON结果。
///
公共密封类EmptyJsonResult:JsonResult
{
public EmptyJsonResult()
{
数据=新的JsonResultData
{
空=真
};
}
}
公共类JsonResultData
{
公共bool Empty{get;set;}
公共字符串[]错误{get;set;}
}
我希望它将{“Empty”:true}
返回到浏览器,但它会返回{“Empty”:true,“Errors”:null}
为了避免在我没有填充的对象上返回null,我是否可以设置任何属性或东西?我认为返回null没有问题,但如果需要实现目标,请使用一个名为JsonResultData的接口和两个实现它的类。其中一个具有您需要返回的所有属性,而第二个中只有空属性。我认为返回null没有问题,但是如果您需要实现您的目标,请使用一个名为JsonResultData的接口和两个实现它的类。其中一个具有您需要返回的所有属性,而第二个中只有空属性。如果您只返回一个anon对象,对您有什么影响
public EmptyJsonResult()
{
Data = new
{
Empty = true
};
}
如果您只是返回一个anon对象,对您有什么影响吗
public EmptyJsonResult()
{
Data = new
{
Empty = true
};
}
您希望填充什么值?您可以为每个对象编写自己的getter,但是null是确定的。您应该查看源代码,看看是否可以更改某些内容。。这里有一个围绕该领域深入研究的问题:我将如何实现
NullValueHandling.Ignore
?您希望填充什么值?您可以为每个对象编写自己的getter,但是null是确定的。您应该查看源代码,看看是否可以更改某些内容。。这里有一个围绕该领域深入研究的问题:如何实现NullValueHandling.Ignore
?