On my Client Side I have an ajax call like below:
$.ajax({
url: "Controller/ListResult",
type: 'POST',
contentType: "application/json;charset=utf-8",
data: JSON.stringify({
Id: ObjectId,
SessionKey: sessionManager.getSessionKey()
}),
dataType: "json",
success: function (result) {
var test = results;
}
}
});
In the Controller I have a method like this :
[HttpPost]
public JsonResult ListResult(string Id, string SessionKey)
{
IBl biz = new BL();
var result = biz.GetResults(Id,SessionKey);
return Json(result);
}
The problem is the result that controller returns is an object which has Enum properties (with their string representation as value). However when it reaches the success function in the ajax call, the enums are no longer string representation, and instead, they have been converted to their int values. How can I avoid this? and keep the string representation on the javascript side.
Instead of returning var result
create some result entity class and you can mark the enum property there with StringEnumConverter.
class Result
{
[JsonConverter(typeof(StringEnumConverter))]
public EnumType EnumProperty { get; set; }
*****other properties goes here****
}
As Stephen suggested this works if one is using Json.NET
as the serializer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With