0

I am trying to send an array to a wcf service.

My javascript :

var listoffice = new Array();

var office1 = { officeid : "1", officename : "Bat Cave" };
var office2 = { officeid : "2", officename : "Robin House" };

listoffice[0] = office1;
listoffice[1] = office2;

$.getJSON("ContactService.svc/createnewAwesomeoffice", { listoffice: listoffice }, function (data) {
   ...           
});

Here's the service :

public struct officetoadd
{
     public string officeid;
     public string officename;
}

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public void createnewAwesomeoffice(List<officetoadd> listoffice)
{
   ...
}

the problem is the listoffice(in the service) is always null. Am I missing something ?

2 Answers 2

2

I think you need to put RequestFormat as well in your WebGetAttribute... Also, you may want to try turning "officetoadd" into a class and decorate it with DataContract and DataMember Attributes.

[DataContract]
public class officetoadd
{
     [DataMember]
     public string officeid;
     [DataMember]
     public string officename;
}

[OperationContract]
[WebGet(RequestFormat - WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void createnewAwesomeoffice(List<officetoadd> listoffice)
{
   ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ty a lot for your suggestion, but I simply use JSON.stringify.
Can I set the parameter as IEnumerable<T> or any other interface or a class that is more primitive than List<T>?
1

Ok, I found a solution by myself. I think this is pertinent to write it here, so it may help other people.

I simply had to convert my array to JSON with this :

var arrayjson = JSON.stringify(listoffice);

And then pass it to the WCF service :

$.getJSON("ContactService.svc/createnewAwesomeoffice", { listoffice: arrayjson }, function (data) {
   ...           
});

Note: The JSON object is now part of most modern web browsers (IE 8 & above).

Comments

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.