Deserialize Cosmos API sql query results in ASP. NET

Viewed 14

I'm working on the ASP.NET Cosmos template. I change the Model in:

{
    using System.ComponentModel.DataAnnotations;
    using Newtonsoft.Json;

    public class Richieste
    {
        [JsonProperty(PropertyName="id")]
        public string Id { get; set; }

        [JsonProperty(PropertyName = "name")]
        public string? Name { get; set; }

        [JsonProperty(PropertyName = "contact")]
        public  IList<Contact>? Contacts { get; set; }


        [JsonProperty(PropertyName ="regdate")]
        [DataType(DataType.Date)]
        public DateTime? RegDate { get; set; }

        [JsonProperty(PropertyName = "description")]
        public string? Description { get; set; }

        [JsonProperty(PropertyName ="note")]
        [DataType(DataType.MultilineText)]
        public string? Note { get; set; }
    }
    public class Contact
    {
        [JsonProperty(PropertyName = "contacttype")]
        public string? ContactType { get; set; }
        [JsonProperty(PropertyName = "contactvalue")]
        public string? ContactValue { get; set; }
    }
}

My services for indexing are:

        public async Task<IEnumerable<Richieste>> GetItemsAsync(string queryString)
    {
        var query = this._container.GetItemQueryIterator<Richieste>(new QueryDefinition(queryString));
        List<Richieste> results = new List<Richieste>();

        while (query.HasMoreResults)
        {
            var response = await query.ReadNextAsync();

            results.AddRange(response.ToList());
        }

        return results;
    }

and my controller is:

        // GET: RichiesteController
    [ActionName("Index")]
    public async Task<ActionResult> Index()
    {
        return View(await _context.GetItemsAsync("SELECT * FROM c"));
    }

When I call the Index method in Controller I receive a json deserialization error.

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[CIS5.Models.Contact]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '[44].contact.contacttype', line 1, position 15982.

How should I deserialize my model?

0 Answers
Related