Search code examples
c#asp.net-coreblazorwebassembly

Why does ReadFromJsonAsync() method return object properties with null values?


I have a Blazor WebAssembly App (ASP.Net Core Hosted, Progressive Web App) with the following logic:

Client:

protected override async Task OnInitializedAsync() {
    string token = await _loginService.GetToken();

    _http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

    var result = await _http.PostAsJsonAsync("api/api/getcalllogs", userCallsRequestFilters);

    if (result.IsSuccessStatusCode) {
        var morUserCallLogs = await result.Content.ReadFromJsonAsync < MorUserCallLogsResponse > ();

        await js.InvokeAsync < object > ("TestDataTablesAdd", "#example");
    } else {
        morUserCallLogs = new MorUserCallLogsResponse();
    }
}

Server: (Server side API I have the following code which works as expected:)

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class MorApiController : ControllerBase
...

[HttpPost("getcalllogs")]
public async Task<MorUserCallLogsResponse> GetCallLogs ([FromBody] MorUserCallsRequestFilters filters)
{
...
return result;

Server side controller API populates the model ok and when I inspect I see the following snap (*some values have been blanked out for security) enter image description here

Model: (My MorUserCallLogsResponse model looks like this:)

namespace MyNumberV2.Model
{
    public class MorUserCallLogsResponse
    {
        public MorUserCallLogsResponse()
        { 
            Calls = new List<MorCall>();
        }
        public string Error { get; set; }
        public bool IsSuccessfull { get; set; }
        public string userid;
        public string username;
        ...
        ...
        public List<MorCall> Calls { get; set; }
        public class MorCall
        {
            public string calldate2;
            public string timezone;
            ...
            ...
        }
    }
}

Back to blazor and when I try to read this returned object on the following line:

var morUserCallLogs = await result.Content.ReadFromJsonAsync<MorUserCallLogsResponse>();

My retrieved model looks like this: enter image description here

As you can see my retrieved model contains all all properties with 140 nested call object models, however all properties are NULL...


Solution

  • I have forgotten to add get; set; for ALL my model properties...

    { get; set; }

      public string Error { get; set; }
      public bool IsSuccessfull { get; set; }
      public string userid { get; set; }
      public string username { get; set; }
      .....