Last post Mar 05, 2021 03:45 AM by wavemaster
Contributor
2619 Points
2753 Posts
Feb 28, 2021 09:51 PM|wavemaster|LINK
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; } }
All-Star
58464 Points
15788 Posts
Mar 01, 2021 12:58 AM|bruce (sqlwork.com)|LINK
I suspect the webapi you are calling does not return the nested results.
Mar 01, 2021 03:15 AM|wavemaster|LINK
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>();
Mar 05, 2021 03:45 AM|wavemaster|LINK
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?
Contributor
2619 Points
2753 Posts
interating over a collection in Blazor Server
Feb 28, 2021 09:51 PM|wavemaster|LINK
recordDetails.Count() = 3
In my UI I expected to do something along these lines:
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.
All-Star
58464 Points
15788 Posts
Re: interating over a collection in Blazor Server
Mar 01, 2021 12:58 AM|bruce (sqlwork.com)|LINK
I suspect the webapi you are calling does not return the nested results.
Contributor
2619 Points
2753 Posts
Re: interating over a collection in Blazor Server
Mar 01, 2021 03:15 AM|wavemaster|LINK
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;
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?
Contributor
2619 Points
2753 Posts
Re: interating over a collection in Blazor Server
Mar 05, 2021 03:45 AM|wavemaster|LINK
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:
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?