尝试以其他格式(如字符串)传递数据,然后它应该可以正常工作。
试试这个代码 -
首先创建一个这样的类 -
public class TimeFormatter : DateTimeConverterBase
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return DateTime.Parse(reader.Value.ToString());
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((DateTime)value).ToString("yyyy/MM/dd"));//format for date
}
}
使用此代码将 Json 数据返回到您的视图 -
JsonSerializerSettings jSettings = new JsonSerializerSettings()
{
Formatting = Formatting.Indented,
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
jSettings.Converters.Add(new TimeFormatter());
JsonSerializer.Create(jSettings);
return JsonConvert.SerializeObject(data, jSettings);//'data' is list of objects to return to view.
不要忘记包含命名空间 -
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;