Create the separate POCO request/response models for the json.

sblb 1,231 Reputation points
2022-12-12T19:54:17.973+00:00

Hi, I try to implement the Foreign Key in my app to link several actions. So I defined the class model ActionItem with main table Developer.
I used to one-to-many relationship, which more appropriate.
When I run app the ActionItem is null and I have the status 404.
I wrote in other post and I received the answer that to solve and fix the issue I have to create the separate POCO request/response models for the json. I really need some advise to do that.

I will appreciate some help to fix the issue ? I know is the basic item but I really neeed to implement this foreign key.

I put directly the github link AppWebFK.

Developer technologies | .NET | Blazor
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. AgaveJoe 30,971 Reputation points
    2022-12-12T22:50:47.373+00:00

    The problem is not creating a POCO. The problem is your Blazor client design does not submit a developerId with the ActionItem! The developerId is needed so you can add or edit an ActionItem. While I agree the Web API data interface should use POCOs not entities, it does not change the fact that a developerId - the foreign key - is needed to add the action item to the correct developer.

    You really need to go through a few beginning level tutorials before moving forward.

    POCO

        public class ActionItemPoco  
        {  
            public string? Tilte { get; set; }  
            public string? Description { get; set; }  
            public string? State { get; set; }  
            public DateTime? OpenDate { get; set; }  
            public DateTime? PlanDate { get; set; }  
            public DateTime? CloseDate { get; set; }  
      
            public int DeveloperId { get; set; }  
        }  
    

    Action

            // POST api/<ActionItemController>  
            [HttpPost]  
            public async Task<ActionResult<ActionItemPoco>> Post([FromBody] ActionItemPoco poco)  
            {  
                Developer? developer = await _context.Developers  
                    .Include(a => a.ActionItems)  
                    .FirstOrDefaultAsync(d => d.DeveloperId == poco.DeveloperId);  
      
                if (developer == null)  
                {  
                    return NotFound();  
                }  
      
                ActionItem ac = new ActionItem()  
                {  
                    ActionItemId = 0,  
                    DeveloperId = poco.DeveloperId,  
                    CloseDate = poco.CloseDate,  
                    Description = poco.Description,  
                    OpenDate = poco.OpenDate,  
                    PlanDate = poco.PlanDate,  
                    State = poco.State,  
                    Tilte = poco.Tilte  
                };  
      
                developer.ActionItems.Add(ac);  
                await _context.SaveChangesAsync();  
      
                return CreatedAtAction(nameof(GetActionItem), new { id = ac.ActionItemId }, poco);  
            }  
    

    JSON

    {  
      "actionItemId": 0,  
      "tilte": "Action Title 2",  
      "description": "Description 2",  
      "state": "State 2",  
      "openDate": "2022-11-18",  
      "planDate": "2022-11-18",  
      "closeDate": "2022-11-18",  
      "developerId": 1  
    }  
    
    1 person found this answer helpful.

  2. sblb 1,231 Reputation points
    2022-12-19T17:27:41.193+00:00

    If you have the solution it will be great if you can tell me how I can fix my problem?
    Really it will be great !!

    0 comments No comments

  3. sblb 1,231 Reputation points
    2022-12-18T10:56:25.39+00:00

    Now, in action table I found teh data from Developer. Really I don't understand!!

    @Aidan Wick {

        [Parameter] public int developerId { get; set; }  
        [Parameter] public int ActionId { get; set; }   
      
        Developer dev = new Developer();  
      
        ActionItem[]? action { get; set; }  
          
        ActionItem act = new ActionItem();  
      
        RadzenDataGrid<ActionItem>? gridaction;  
             
         
        protected async override Task OnParametersSetAsync()  
        {  
            dev = await http.GetFromJsonAsync<Developer>($"api/developer/{developerId}");  
            
        }  
      
        async Task EditDeveloper()  
        {  
            await http.PutAsJsonAsync("api/developer", dev);  
            await http.GetAsync($"api/developer/GenearteEcoById/{developerId}");       
            await js.InvokeVoidAsync("alert", $"Updated Successfully!");  
            uriHelper.NavigateTo("developer");          
        }    
      
         protected override async Task OnInitializedAsync()  
        {  
           action = await http.GetFromJsonAsync<ActionItem[]>("api/developeractions");  
        }        
      
        async Task CreateActionList()  
        {          
         act.DeveloperId =  developerId;      
         await http.PutAsJsonAsync($"api/developeractions", act);        
         uriHelper.NavigateTo($"/developer/edit/{developerId}");  
        }  
      
    ..  
    }  
    

    Act is well populate but it's not rendering

    271842-image.png


  4. sblb 1,231 Reputation points
    2022-12-14T13:16:33.903+00:00

    Yes the github wan't updated. I just updated github. I taken into accoiunt your last comment; I've always the same problem.

    I remarks there is a problem with act in Edit.razor with the message :

    Erreur (active)	CS1503	Argument 1 : conversion impossible de 'AppWeb.Shared.Models.ActionItem' en 'System.Collections.Generic.IEnumerable<AppWeb.Shared.Models.ActionItem>'	   
    

    AppWeb.Client C:\App\sbd\AppWeb_v1\AppWeb\Client\Pages\Developer\Edit.razor 32

    How I can fix this error?
    I think that's not the main problem.


  5. sblb 1,231 Reputation points
    2022-12-13T16:17:50.543+00:00

    ben yes !!
    in the @page "/developer/edit/{developerId:int}" I rendering the data from Developer class and in the same page I want to redering all ActionItem associate to DeveloperId.
    In my case the DeveloperId is the Foreign Key that associate all items from ActionItems Class.

    So I didn't see what I've to change in Edit.razor.


Your answer

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