0

at first sorry for my English. I am a beginner in Microsoft web technologies. Currently, I am working on a shopping cart project, and facing the problem given below. Thanks in advance.

Here are my models.

public class Shipment
    {
        public Guid Id { get; set; }
        //Other 
        public List<OrderedProduct> OrderedProduct { get; set; }
    }



public class OrderedProduct
    {
        public Guid Id { get; set; }

        //other
        public Guid ShipmentId { get; set; }
        public Shipment Shipment { get; set; }

    }

In my Controller.

 var result = _dbContex.Shipments.Include(X => X.OrderedProduct).ToListAsync();
  return new OkObjectResult(result);

It gives me wrong JSON object result.

[{...... "orderedProduct":[{...... end
Jaggesher Mondal
  • 193
  • 2
  • 10
  • Could you create a [mcve] rather than just a snippet? It's hard to tell what's wrong with the various "..." involved, and without being able to run the code ourselves. Are you able to reproduce the problem *without* database interaction, by just hard-coding some data? – Jon Skeet Apr 17 '18 at 16:14
  • sure, let me try. Thanks :) – Jaggesher Mondal Apr 17 '18 at 16:16
  • 1
    I suspect an exception is getting thrown during serialization while writing to the output stream, leaving the results truncated. For instance, see https://github.com/aspnet/Mvc/issues/4160. Can you try logging the error by using the [`JsonSerializerSettings.Error`](https://www.newtonsoft.com/json/help/html/SerializationErrorHandling.htm#ErrorEvent) event? See [JsonSerializerSettings and Asp.Net Core](https://stackoverflow.com/q/35772387) for instructions on modifying settings in asp.net core. – dbc Apr 17 '18 at 17:34

2 Answers2

2

Finally, I got It. The problem was a self-referencing loop for the navigation property of the model OrderedProduct. Because OrderProduct model has a navigation property named like Shipment model.

public class OrderedProduct
    {
        public Guid Id { get; set; }

        //other
        public Guid ShipmentId { get; set; }
        public Shipment other { get; set; }//Change

    }
Jaggesher Mondal
  • 193
  • 2
  • 10
0

You retrieve asynchronous and you must use ”await” https://msdn.microsoft.com/en-us/library/hh191443(v=vs.120).aspx or change ToListAsync to ToList

Paweł Haracz
  • 52
  • 1
  • 3
  • Thanks, but that is not my problem. Retrieving data is not my problem, converting it into proper JSON is my problem. – Jaggesher Mondal Apr 17 '18 at 16:24
  • so, you need to change serialization format in a configuration your application, [link](https://codeteddy.com/2017/06/06/create-api-with-asp-net-core-day-4-working-with-serializer-settings-and-content-negotiation-in-asp-net-core-api/) – Paweł Haracz Apr 17 '18 at 16:30
  • I think you still need the `await` otherwise you are returning the Task instead of the data. – Leonardo Henriques Apr 17 '18 at 16:50
  • @LeonardoHenriques Thanks, In my code, I am using await, still same problem. Assume, data is ready, now please give me some clue to solve JSON object result error. TIA – Jaggesher Mondal Apr 17 '18 at 16:56