How invoke a child data in List ?

sblb 1,231 Reputation points
2023-08-20T07:44:42.43+00:00

Hi,

I would like to invoke the GET method to obtain child data (ActionItems) in parent List (SuiviBEs)

The structure of the data

public class SuiviBE
    {
        [Key]
        public int SuiviBEId { get; set; } 
		...
        public List<ActionItem>? ActionItems { get; set; }        

    }
  

    public class ActionItem
    {
        public int ActionItemId { get; set; }
	    ...
        public int SuiviBEId { get; set; }
        [JsonIgnore]
        public SuiviBE? SuiviBE { get; set; }
    }


For suiviBEId = 2, I obtain the parent data et the child data User's image

Razor page :

<EditForm Model="@dev" OnValidSubmit="@OnValidSubmit">   
  <DataAnnotationsValidator />

<RadzenDataList Data="@dev?.ActionItems" TItem="ActionItem" PageSize="5" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true">
       <Template Context="dev">                                          
          <RadzenColumn Size="12" SizeLG="2" Class="rz-p-4">
	          <RadzenButton Icon="edit" ButtonStyle="ButtonStyle.Secondary" 		   Size="ButtonSize.Small" 
Click="@(args => EditAction(dev.ActionItemId))" @onclick:stopPropagation="true" />                              
                </RadzenColumn>
         </Template>
        </RadzenDataList>

</EditForm>

@code{

.....
   [Parameter] public SuiviBE? dev { get; set; }
....

 async Task EditAction(int actionId)
  { 
act.ActionItemId = actionId;         
dev = await client.GetFromJsonAsync<SuiviBE> ($"api/etude/{dev?.ActionItems}");

..

 var test = await DialogService.OpenAsync("Editing action", de =>                    @<div>   

)
}

I would like to Get the list of the ActionItems e.g with ActionItemId = 3002 to edit the data.

I tried :

dev = await client.GetFromJsonAsync<SuiviBE> ($"api/etude/{dev?.ActionItems}");
dev = await client.GetFromJsonAsync<SuiviBE>($"api/etude/{dev.SuiviBEId}")
       .Where(a => a.ActionsItems = dev.ActionItemId).ToList();

after several attempts I can't get the data .

Could you please help me to write a good line of the code to obtain child data from SuiviBE?

Thanks in advance to your help!!

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,672 questions
{count} votes

Accepted answer
  1. AgaveJoe 30,031 Reputation points
    2023-08-20T17:25:10.5866667+00:00

    I will appreciate your help, because I don't know how I can obtain child data from parent data list

    Study the Entity Framework documentation link below. The LINQ pattern to filter child records is straight forward.

    Filtered include

    Sample JSON based on the model in your initial post.

    [
      {
        "suiviBEId": 1,
        "actionItems": [
          {
            "actionItemId": 1,
            "suiviBEId": 1
          },
          {
            "actionItemId": 2,
            "suiviBEId": 1
          }
        ]
      },
      {
        "suiviBEId": 2,
        "actionItems": [
          {
            "actionItemId": 3,
            "suiviBEId": 2
          },
          {
            "actionItemId": 4,
            "suiviBEId": 2
          }
        ]
      }
    ]
    

    Controller example that filters child records.

    namespace WebApiSqlite.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class EtudeController : ControllerBase
        {
            private readonly ILogger<EtudeController> _logger;
            private readonly ApplicationDbContext _context;
            public EtudeController(ILogger<EtudeController> logger, ApplicationDbContext context)
            {
                _logger = logger;
                _context = context;
            }
            [HttpGet]
            public async Task<IActionResult> Get()
            {
                List<Models.SuiviBE> dev = await _context.SuiviBEs
                    .Include(s => s.ActionItems).ToListAsync();
    
                return Ok(dev);
            }
    
            [HttpGet("{suiviBEId}/{actionItemId}")]
            public async Task<IActionResult> Get(int suiviBEId, int actionItemId)
            {
                Models.SuiviBE? dev = await _context.SuiviBEs.Include(
                    a => a.ActionItems
                    .Where(a => a.ActionItemId == actionItemId)
                    ).FirstOrDefaultAsync(s => s.SuiviBEId == suiviBEId);
    
                return Ok(dev);
            }
        }
    }
    

    Filtered results and URL

    https://localhost:7250/api/Etude/2/3
    
    {
      "suiviBEId": 2,
      "actionItems": [
        {
          "actionItemId": 3,
          "suiviBEId": 2
        }
      ]
    }
    

    Routing to controller actions in ASP.NET Core

    If you only want a single ActionItem, then write a query to fetch the ActionItem by Id.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.