I've overwritten the default Json Serializer from ASP.NET MVC with:
public class JsonNetResult : JsonResult
{
public JsonNetResult()
{
Settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Error,
};
}
public JsonSerializerSettings Settings { get; private set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("JSON GET is not allowed");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
if (this.ContentEncoding != null)
response.ContentEncoding = this.ContentEncoding;
if (this.Data == null)
return;
var scriptSerializer = JsonSerializer.Create();
using (var sw = new StringWriter())
{
scriptSerializer.Serialize(sw, this.Data);
response.Write(sw.ToString());
}
}
}
And when I am Serializing the Following:
public JsonResult GetLevels()
{
List<ListItem> items = new List<ListItem>();
items.Add(new ListItem() { Text = "Home", Value = "5"});
items.Add(new ListItem() { Text = "Live", Value = "6"});
items.Add(new ListItem() { Text = "Dev", Value = "7"});
items.Add(new ListItem() { Text = "Staging", Value = "8"});
return Json(items, JsonRequestBehavior.AllowGet);
}
The JavaScript Object I get is the following:
Levels: Array[4] 0: "Home" 1: "Live" 2: "Dev" 3: "Staging"
So here all my Informations are lost like value and so on. But when I use the Default Json serializer then I got the "right" Informations serialized
Levels: Array[4]
0: Object Attributes: Object Enabled: true Selected: false Text: "Home" Value: "5"
1: Object Attributes: Object Enabled: true Selected: false Text: "Live" Value: "6" ...
but I need to use the custom serializer because of the DateTime serialization. But I don't know what I am doing wrong with the custom JsonNetResult or what I am missing. Because it can't be normal that here is Data missing or?