Function Jobs() As List(Of JobsViewmodel)
Return dbContext.Jobs.Select(Function(job) New JobsViewmodel With {.JobID = job.ID, .Description = job.Description}).ToList
End Function
This code is ripped from my other app where it works fine. Debugging the Jobs controller action shows that the return value is clearly a fully instantiated list of 10 jobsViewModel objects. But instead of JSON, I get that string.
I have also tried veersion such as
Function Jobs() As IEnumerable(Of JobsViewmodel)
Return dbContext.vJobsVerboses.Select(Function(job) New JobsViewmodel With {.JobID = job.ID, .Description = job.Description})
End Function
Function Jobs() As List(Of JobsViewmodel)
Return dbContext.Jobs.Select(Function(job) New JobsViewmodel With {.JobID = job.ID, .Description = job.Description}).ToList()
End Function
Sorry, but I believe your code suggestion is identical to my posted example.
I am using ToList. (If you are asking me to add the () paretheses to the end of ToList, i.e. ToList() instead of ToList, this makes no difference in VB. And I tried that.)
And as I stated, during debugging, inspecting the return value shows that my statement is returning a fully instantiated list of objects.
Also, this is the exact code ripped form a webservice that works perfectly, and returns a full JSON list.
I would take a look at your configurations for your json formatter. I had an issue similar to this and solved it by setting the TypeNameHandling like this:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects;
json.SerializerSettings.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
Scattertasti...
0 Points
2 Posts
Problems Serializing to JSON
Jan 02, 2013 02:36 AM|LINK
I am converting a VB.net project from WCF to Web API.
Instead of JSON I am getting the string
"System.Collections.Generic.List`1[Activision.JobsViewmodel]"
My controller function clearly gets a fully formed list, but it's not serialized.
Calling javascript
$.ajax({ type: "POST", url: "../Home/Jobs", dataType: 'json', contentType: "application/json; charset=utf-8", success: function (j) { //... } });The controller action:
Function Jobs() As List(Of JobsViewmodel) Return dbContext.Jobs.Select(Function(job) New JobsViewmodel With {.JobID = job.ID, .Description = job.Description}).ToList End FunctionThis code is ripped from my other app where it works fine. Debugging the Jobs controller action shows that the return value is clearly a fully instantiated list of 10 jobsViewModel objects. But instead of JSON, I get that string.
I have also tried veersion such as
Function Jobs() As IEnumerable(Of JobsViewmodel) Return dbContext.vJobsVerboses.Select(Function(job) New JobsViewmodel With {.JobID = job.ID, .Description = job.Description}) End Functionbut with no success.
urenjoy
Star
13531 Points
2035 Posts
Re: Problems Serializing to JSON
Jan 02, 2013 04:43 AM|LINK
Make sure you are using ToList()
Try following:
Function Jobs() As List(Of JobsViewmodel) Return dbContext.Jobs.Select(Function(job) New JobsViewmodel With {.JobID = job.ID, .Description = job.Description}).ToList() End FunctionScattertasti...
0 Points
2 Posts
Re: Problems Serializing to JSON
Jan 02, 2013 05:13 PM|LINK
Sorry, but I believe your code suggestion is identical to my posted example.
I am using ToList. (If you are asking me to add the () paretheses to the end of ToList, i.e. ToList() instead of ToList, this makes no difference in VB. And I tried that.)
And as I stated, during debugging, inspecting the return value shows that my statement is returning a fully instantiated list of objects.
Also, this is the exact code ripped form a webservice that works perfectly, and returns a full JSON list.
dmschmalz
Member
16 Points
3 Posts
Re: Problems Serializing to JSON
Jan 03, 2013 09:43 PM|LINK
I would take a look at your configurations for your json formatter. I had an issue similar to this and solved it by setting the TypeNameHandling like this:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects; json.SerializerSettings.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;