Forgive me if I'm doing this all wrong, but I'm trying to write a web service that will allow someone to post JSON data, which the service wil convert to entity framework objects, and then save to the database.
I added a WebServiceController class to my controllers, and wrote the following method...
[HttpPost]
[ActionName("AddSession")]
public JsonResult AddSession(Session S) {
// do stuff here
}
I wrote a test HTML page that used jQuery to build a JSON object that had the same members as my Session entity (which comes from the EF model). When I post the JSON to the web service method, I get a populated Session entity, with the data from the HTML page.
Well, almost. The simple properties on the Session entity are populated fine, but the child entities are not. For example, the Session contains a DateTime property cunningly named Date, and this comes through fine. However, it also has a property which is a User entity (also from the EF model). I set the User property in the JSON, but when the web service gets the post, the User is null.
As you can see, I'm just building a hard-coded object for testing, then displaying the JSON in a DIV on the page before posting it to the web service and displaying the message that comes back. The JSON displayed on the page looks like this...
This looks fine, and is valid according to a couple of JSON parsers I found around.
Now, in the web service code, the User property of the Session object is not null, but the values I set in the jQuery are ignored, so the ID is zero and the NhsNumber is null (defaults for an int and a string).
What am I doing wrong? More to the point, how do I send the child object to the web service?
Thanks for any help you can give.
If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
If you're going to send arbitrary complex data structures to the server, you want to send it as Content-Type:"application/json". With $.post you're sending the data as url-form-encoded and so you won't get the hierarchy.
I tried putting a breakpoint on the first line of the web service method (AddSession), but it was never hit, so I'm obvously doing something wrong with the AJAX call itself. I tried using the full URL of the web service, http://localhost:61431/WebService/AddSession
but that didn't help.
Any ideas? Thanks again.
If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
MrYossu
Member
134 Points
144 Posts
Child objects are null when posting JSON data to my MVC web service
May 31, 2012 03:35 PM|LINK
Hello,
Forgive me if I'm doing this all wrong, but I'm trying to write a web service that will allow someone to post JSON data, which the service wil convert to entity framework objects, and then save to the database.
I added a WebServiceController class to my controllers, and wrote the following method...
[HttpPost] [ActionName("AddSession")] public JsonResult AddSession(Session S) { // do stuff here }I wrote a test HTML page that used jQuery to build a JSON object that had the same members as my Session entity (which comes from the EF model). When I post the JSON to the web service method, I get a populated Session entity, with the data from the HTML page.
Well, almost. The simple properties on the Session entity are populated fine, but the child entities are not. For example, the Session contains a DateTime property cunningly named Date, and this comes through fine. However, it also has a property which is a User entity (also from the EF model). I set the User property in the JSON, but when the web service gets the post, the User is null.
Here is some jQuery I used for testing...
<script type="text/javascript"> $("#theForm").submit(function(event) { event.preventDefault(); var sessionData = { "ID": 1, "UserID": 2, "User": { "ID": 2, "NhsNumber": "1234567890" }, "CourseID": 3, "Date": "7 Apr 1961 16:20" }; $('#json').html(JSON.stringify(sessionData)); $.post("/WebService/AddSession", sessionData, function(data) { $('#results').html("Result was OK: " + data.ok + "<br/>message was: " + data.message); }, "json"); } ); </script>As you can see, I'm just building a hard-coded object for testing, then displaying the JSON in a DIV on the page before posting it to the web service and displaying the message that comes back. The JSON displayed on the page looks like this...
{"ID":1,"UserID":2,"User":{"ID":2,"NhsNumber":"1234567890"},"CourseID":3,"Date":"7 Apr 1961 16:20"}This looks fine, and is valid according to a couple of JSON parsers I found around.
Now, in the web service code, the User property of the Session object is not null, but the values I set in the jQuery are ignored, so the ID is zero and the NhsNumber is null (defaults for an int and a string).
What am I doing wrong? More to the point, how do I send the child object to the web service?
Thanks for any help you can give.
BrockAllen
All-Star
27530 Points
4905 Posts
MVP
Re: Child objects are null when posting JSON data to my MVC web service
May 31, 2012 03:39 PM|LINK
If you're going to send arbitrary complex data structures to the server, you want to send it as Content-Type:"application/json". With $.post you're sending the data as url-form-encoded and so you won't get the hierarchy.
So use this instead of $post:
$.ajax({
url : "...",
data : JSON.stringify(sessionData),
contentType : "application/json",
success : function(result) {
...
}
});
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
MrYossu
Member
134 Points
144 Posts
Re: Child objects are null when posting JSON data to my MVC web service
May 31, 2012 04:02 PM|LINK
Hello,
Thanks for the quick reply, but unfortunately, it's not working at all now! I changed the code as shown below, but the return JSON is aways empty...
<script type="text/javascript"> $("#theForm").submit(function(event) { event.preventDefault(); var s = { "ID": 1, "UserID": 2, "User": { "ID": 2, "NhsNumber": "1234567890" }, "CourseID": 3, "Date": "7 Apr 1961 16:20" }; $('#json').html(JSON.stringify(s)); $.ajax({ url: "/WebService/AddSession", data: JSON.stringify(s), contentType: "application/json", success: function(data) { $('#results').html(JSON.stringify(data)); }, error: function(data) { $('#results').html(JSON.stringify(data)); } } ); }); </script>I tried putting a breakpoint on the first line of the web service method (AddSession), but it was never hit, so I'm obvously doing something wrong with the AJAX call itself. I tried using the full URL of the web service, http://localhost:61431/WebService/AddSession but that didn't help.
Any ideas? Thanks again.
BrockAllen
All-Star
27530 Points
4905 Posts
MVP
Re: Child objects are null when posting JSON data to my MVC web service
May 31, 2012 04:06 PM|LINK
There are other settings you probably need on the ajax call:
$.ajax({
...,
type : "POST",
dataType:"json"
});
there might be others depending on your situation. I'd suggest reading the docs to make sure you didn't miss any.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
MrYossu
Member
134 Points
144 Posts
Re: Child objects are null when posting JSON data to my MVC web service
May 31, 2012 04:07 PM|LINK
OK, my mistake. My web service accepts a POST, not a GET, and I needed to set that in the call to .ajax as well. Once I dd that, it worked fine :)
Thanks very much again.
MrYossu
Member
134 Points
144 Posts
Re: Child objects are null when posting JSON data to my MVC web service
May 31, 2012 04:07 PM|LINK
Oops, didn't see your reply before I posted!