On my Client Side I have an ajax call like below:
在我的客户端,我有一个像下面这样的ajax调用:
$.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 :
在Controller中,我有一个这样的方法:
[HttpPost]
public JsonResult ListResult(string Id, string SessionKey)
{
IBl biz = new BL();
var result = biz.GetResults(Id,SessionKey);
return Json(result);
}
The Problem is that the result that controller returns is an object which has few 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 somehow have been converted to their int values. How can I avoid this? and keep the string representation on the javascript side.
问题是控制器返回的结果是一个具有很少枚举属性的对象(其字符串表示为值)。但是当它在ajax调用中到达success函数时,枚举不再是字符串表示,而是以某种方式将它们转换为它们的int值。我怎么能避免这个?并保持字符串表示在JavaScript方面。
2 个解决方案
#1
6
Instead of returning var result
create some result entity class and you can mark the enum property there with StringEnumConverter.
而不是返回var结果创建一些结果实体类,您可以使用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.
正如Stephen建议的那样,如果使用Json.NET作为序列化程序,则可行。
#2
3
Try something like this:
尝试这样的事情:
var result = biz.GetResults(Id,SessionKey);
var modifiedResult = new
{
...,
r.EnumValue.ToString(),
...
};
return Json(modifiedResult);
#1
6
Instead of returning var result
create some result entity class and you can mark the enum property there with StringEnumConverter.
而不是返回var结果创建一些结果实体类,您可以使用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.
正如Stephen建议的那样,如果使用Json.NET作为序列化程序,则可行。
#2
3
Try something like this:
尝试这样的事情:
var result = biz.GetResults(Id,SessionKey);
var modifiedResult = new
{
...,
r.EnumValue.ToString(),
...
};
return Json(modifiedResult);