locked
interating over a collection in Blazor Server RRS feed

  • Question

  • User379720387 posted

    private IEnumerable<RecordDetail> recordDetails = new List<RecordDetail>();
    
    recordDetails = await httpClient.GetFromJsonAsync<RecordDetail[]>($"{baseUrl}/api/Record/DetailByClient/22/49/3");

    recordDetails.Count() = 3

    In my UI I expected to do something along these lines:

    @foreach (var detail in recordDetails)
    {
       in here  detail.TransactionId and detail.Service


    }

    And since RecordDetail has a collection of ServiceItems, I was expecting to have another foreach to step through each ServiceItem.

    None of this seems to work, as detail.? has none of the properties I was expecting to see.

    Not understanding this.

    public class RecordDetail
        {
            public int TransactionId { get; set; }
            public string CName { get; set; }
            public string TDate { get; set; }
            public string BDate { get; set; }
            public string Initials { get; set; }
            public bool IsBilled { get; set; }
            public bool IsPaid { get; set; }
            public string SvcLevel { get; set; }
            
            public List<ServiceItem> ServiceItems { get; set; }
    
        }

    Sunday, February 28, 2021 9:51 PM

All replies

  • User-474980206 posted

    I suspect the webapi you are calling does not return the nested results. 

    Monday, March 1, 2021 12:58 AM
  • User379720387 posted

    I have removed the nested item from the controller and the model, just to keep things simple.

    Still the same issue. I cannot see what the Blazor server page sees.

    Only thing I am sure of what the controller does with a breakpoint sitting at return recordDetails;

    recordDetails
    Count = 3
        [0]: {BtApiEf5.Model.Custom.RecordDetail}
        [1]: {BtApiEf5.Model.Custom.RecordDetail}
        [2]: {BtApiEf5.Model.Custom.RecordDetail}
    recordDetails[0]
    {BtApiEf5.Model.Custom.RecordDetail}
        BDate: "May  9 2020  4:54PM"
        CName: "Jim"
        Initials: "JMC"
        IsBilled: false
        IsPaid: false
        SvcLevel: "R1"
        TDate: "May  9 2020  4:54PM"
        TransactionId: 1015

    I am in the dark what happens on the razor page, the only thing I can see at the breakpoint is recordDetails.Count() = 3

    Is this the correct return type?

    private IEnumerable<RecordDetail> recordDetails = new List<RecordDetail>();

    Monday, March 1, 2021 3:15 AM
  • User379720387 posted

    I have separated the data retrieval from the deserialization.

    Now I can confirm that the data makes it from the controller into JsonSerializer.Deserialize, but it doesn't produce the desired result.

    var json = JsonSerializer.Serialize(recordDetails);
                var content = new StringContent(json);
                string errorMessage = null;
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = await httpClient.GetAsync($"{baseUrl}/api/Record/DetailByClient/22/49/3");
                string dtls = await response.Content.ReadAsStringAsync();
                      
                if (dtls is not null)
                {
                    recordDetails = JsonSerializer.Deserialize(dtls, typeof(List<RecordDetail>)) as List<RecordDetail>;
                }
                else
                {
                    
                }

    dtls look like this:

    [{"TransactionId":1015,"CName":"Doolin","TDate":"May  9 2020  4:54PM","BDate":"May  9 2020  4:54PM","Initials":"JMC","IsBilled":false,"IsPaid":false,"SvcLevel":"R1"},{"TransactionId":988,"CName":"Doolin","TDate":"Jan 23 2020  3:13PM","BDate":"Mar 10 2020 11:17AM","Initials":"JMC","IsBilled":false,"IsPaid":false,"SvcLevel":"R1"},{"TransactionId":974,"CName":"Doolin","TDate":"Jan 22 2020  2:36PM","BDate":"Jan 22 2020  2:36PM","Initials":"JMC","IsBilled":false,"IsPaid":false,"SvcLevel":"R1"}]

    RecordDetail.cs is in the OP.

    I have looked up "deserialize not working" with suggestions to check the capitalization, which seems ok to me.

    Anything I need to look at?

    Friday, March 5, 2021 3:45 AM