0

I Have a USerBO like this

  [Serializable]
  public class UserBO
  {
    public string userId      { get; set; }
    public string userCode    { get; set; }
    public string userGroup   { get; set; }
  }

Have retrieved this Object from session. Now I need to pass this Object (USerBO) from javascript to a C# webmethod using JSON. Is it possible ?

3
  • Are you asking for a C# JSON parser? Commented Dec 15, 2011 at 0:10
  • "Is it possible ?" Well, aren't we all intensely curious about this question? Every single day, many of us are writing code and asking ourselves: "Is it? Could it be that this would work?". "Perhaps" is the only answer. It depends. Have we written some code, or are we still staring at the dooming void main, seeking in utter dispair for inspiration? If the latter, I won't give you much hope. But if by any chance you wrote some code, and told us what you found out writing that code, and where you are stuck now, perhaps it would be possible for me to give an upvote. Or wasn't it about that? Commented Dec 15, 2011 at 0:17
  • Usually you retrieve an object from session on the server. Are you looking to return that object in the web-method? Or perhaps take it as an argument to a web-method? Commented Dec 15, 2011 at 0:21

2 Answers 2

1

When you say webmethod, are you using an ASPX page method or ASMX ScriptService? If so, the answer is that it's very easy to send that object from the browser to your method.

A page method like this one:

[WebMethod]
public bool SaveUserBO(UserBO User) {
  // Assuming you had a .Save() method on that class, for example.
  return User.Save();
}

Would automatically hydrate its User parameter if you passed it JSON like this:

{'User':{'userId':42,'userCode':1,'userGroup':2}}
Sign up to request clarification or add additional context in comments.

Comments

0

How to: Serialize and Deserialize JSON Data

1 Comment

Hi Rob,Thanks Have tried using JavaScriptSerializer jss = new JavaScriptSerializer(); UserBO user = jss.Deserialize<UserBO>(userBO); ... there is a problem at deserialization....System.ArgumentException: Invalid object passed in, ':' or '}'....Do I need to serialize the object before posting to webservice?

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.