0

I am trying to pass a StringBuilder message as an extra data back to ajax call but I am failing to manipulate it in client side. Below is the way I append message to StringBuilder and pass it back.

StringBuilder retMessage=new StringBuilder();
retMessage.Clear().AppendLine("Successfully added the user!"); 
//Clear will done only on Success otherwise will append the different errorMessage using AppendLine
return Json(new { result = valid, message = retMessage });

On the ajax Success I tried retrieving it as below but with no success.

 success: function (data) {
              if (data.result) {
                    ResetForm('#frmChangePwd');
                    console.log(data.message);
              },
 }

Below are images showing how it is passed from server side and how it is obtained in client side!!

Server Side return

Server Side return

Client Side retrieval

Client Side retrieval

Can anyone tell me how to retrieve message in client side and display it in view?

6
  • 1
    Did you try calling the to String method on the stringbuilder when you constructed the object literal. Commented May 9, 2015 at 11:55
  • 1
    You need to call .ToString() on the StringBuilder - return Json(new { result = valid, message = retMessage.ToString() }); Commented May 9, 2015 at 11:56
  • I am trying to implement it @StephenMuecke Commented May 9, 2015 at 12:10
  • Thank you @StephenMuecke.. It works well and fine.. :) Commented May 9, 2015 at 12:15
  • 1
    And just by way of explanation, without .ToString() your passing the instance of the StringBuilder so the Json method serializes the instance from its public properties, but the actual text output is internal so its not sent - only the 3 public properties that you see in the browser Commented May 9, 2015 at 12:22

1 Answer 1

1

Call the toString method on the StringBuilder even you create the anonymous object on the return from the server.

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

1 Comment

I will try implementing this and will let you know.

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.