4

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.

6
  • What is sessionManager.getSessionKey()? Commented Nov 3, 2016 at 3:32
  • @StephenMuecke, it is an internal function to retrieve session, but this shouldn't affect anything. Because it works as expected. Commented Nov 3, 2016 at 3:34
  • 4
    You will need to create an anonymous object to represent your data and use ToString() on the enum properties (the JavascriptSerializer serializes enums to their numeric values and not their string representation) Commented Nov 3, 2016 at 3:43
  • Hi Benjamin , Can you try this one out return Json(result.ToList(), JsonRequestBehavior.AllowGet); Commented Nov 3, 2016 at 3:45
  • 3
    You can also use Json.NET with the [JsonConverter(typeof(StringEnumConverter))] attribute on your properties Commented Nov 3, 2016 at 3:52

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Only if OP is using Json.NET as the serializer :)
3

Try something like this:

    var result = biz.GetResults(Id,SessionKey);
    var modifiedResult = new
      {
        ...,
        r.EnumValue.ToString(),
        ...
      };
    return Json(modifiedResult);

4 Comments

As I said the result in the Controller is already String representation, and there is no need to ToString() it. It just gets converted to int value when it reaches to the Javascript side.
If the properties in the result getting returned by the Controller are Enums, the call to return Json(result) will convert those strings back to their integer values. If you project the result first into an anonymous type then the properties will just be strings.
Almost right - but you cannot use .Select() on a single object (which is what OP stated the GetResults() method returns
@Stephen -- thanks for the feedback, answer has been updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.