status code 400 with method PUT

sblb 1,231 Reputation points
2023-04-17T11:29:25.7133333+00:00

Class that generate the status code 400 :

 public class SuiviBE
    {
        [Key]
        public int SuiviBEId { get; set; }
     ...
      pu
        public string? Nadt { get; set; }
       ng? Statut {

            get
            {
             
                {
                    string var1;
                    var1 = "Open";
                    return var1;
                }
                else if (DatePush != null)
                
                    string var2;
                    var2 = "Close";
                    return var2;
                }
                return CloseOn;
            }

            private set { }
        }
     
      
        //  public List<ActionItem>? ActionItems { get; set; }
        public List<ActionItem>? ActionItems { get; set; }
    }
      
    public class Ressource
    {

    }

    public class ActionItem
    {

        public int ActionItemId { get; set; }
        ...
        public string? DescriptionA { get; set; }
        public string? State { get; set; }
        public DateTime? OpenDate { get; set; }
        public DateTime? PlanDate { get; set; }
        public DateTime? CloseDate { get; set; }

        public int SuiviBEId { get; set; }
        public SuiviBE SuiviBE { get; set; }
    }

Etude.razor page :

<RadzenDataGrid @ref="grid" ExpandMode="DataGridExpandMode.Single" SelectionMode="DataGridSelectionMode.Single" @bind-Value=@selectedDeveloper RowRender="@RowRender"
                Data="@developers" TItem="SuiviBE" RowUpdate="@OnUpdateRow" RowCreate="@OnCreateRow" Sort="@Reset" Page="@Reset" Filter="@Reset" ColumnWidth="200px">
  <Template Context="developers">

    <RadzenLabel>Actions</RadzenLabel>
         <SidepanelComponent @ref="createSidepanel" Title="Create" SubTitle="Actions">
            <FormCreateSidePanel ButtonAction="Create" act="@act" OnValidSubmit="@(args => @CreateActionList(developers.SuiviBEId))"/>
        </SidepanelComponent>

        <RadzenButton Text="Create Action" Icon="report" ButtonStyle="ButtonStyle.Light" Click="@(args => @OpenCreateForm(developers.SuiviBEId))" @onclick:stopPropagation="true" />
        <RadzenButton Variant="Variant.Outlined" Click=@Close Text="Light" ButtonStyle="ButtonStyle.Light" />
        <Codeblock Code="@codeblock1" />
        <hr />
       
        
         <RadzenDataGrid @ref="gridaction"  AllowFiltering="true" AllowPaging="true" PageSize="5" AllowSorting="true" EditMode="DataGridEditMode.Single"
                Data="@developers.ActionItems" TItem="ActionItem" CellRender="@CellRender" RowRender="@RowRender" ExpandMode="DataGridExpandMode.Single"
                SelectionMode="DataGridSelectionMode.Single" >
               <Columns>
                   <RadzenDataGridColumn TItem="ActionItem" Property="ActionItemId" Title="ActionItemId" Width="80px"/> 
                   <RadzenDataGridColumn TItem="ActionItem" Property="SuiviBEId" Title="SuiviBEId" Width="80px"/> 
               
               </Columns>
         </RadzenDataGrid> 
</Template>

</RadzenDataGrid> 

@code 
{
    RadzenDataGrid<ActionItem>? gridaction;
    RadzenDataGrid<SuiviBE>? grid; 


  IEnumerable<SuiviBE>? developers;
  ActionItem act = new ActionItem(); 

   protected override async Task OnInitializedAsync()
    {
        developers= await client.GetFromJsonAsync<SuiviBE[]>($"api/etude");     
     
    } 
  
 async Task CreateActionList(int actionId)
    {
        act.ActionItemId = actionId;     
        await client.PutAsJsonAsync($"api/etude/{act.SuiviBEId}", act);       
        uriHelper.NavigateTo($"/etude");
    }      
    
      private void OpenCreateForm(int actionId) 
    {
        act.SuiviBEId = actionId;    
        createSidepanel.Open(); 
    }

}

FormCreateSidePanel

 <EditForm Model="@act"  OnValidSubmit="@OnValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary /> 

        <div class="mb-3">
            <label class="form-label">Primary Classification</label>
            <InputSelect @bind-Value="act.State" class="form-control">
                <option value="">Select State ...</option>
                <option value="Open">Open</option>
                <option value="Close">Closed</option>                
            </InputSelect>
        </div>

        <div class="d-grid gap-2">         
            <button type="submit" class="btn btn-success" >@ButtonAction</button>
            <button class="btn btn-outline-secondary" type="reset">Reset</button>
            <button class="btn btn-outline-danger" type="button" @onclick="Sidepanel.SoftClose">Cancel</button>
        </div>
@code{
  [Parameter] public ActionItem? act { get; set; }
    [Parameter] public EventCallback OnValidSubmit { get; set; }
    [Parameter] public string ButtonAction { get; set; } = "Save";
}

controller


        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var devs = await _context.SuiviBEs.Include(a => a.ActionItems).ToListAsync();
            return Ok(devs);
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> Get(int id)
        {
            var dev = await _context.SuiviBEs.Include(a => a.ActionItems)
                                             .FirstOrDefaultAsync(a => a.SuiviBEId == id);
            return Ok(dev);
        }

       
        public async Task<ActionResult<SuiviBE>> PostAsync([FromBody] SuiviBE developer)
        {
            _context.Add(developer);
            await _context.SaveChangesAsync();
            return CreatedAtAction(nameof(Get), new { id = developer.SuiviBEId }, developer);
        }
        [HttpPut]
        public async Task<IActionResult> Put(SuiviBE developer)
        {
            _context.Entry(developer).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return NoContent();
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<SuiviBE>> Put(int id, [FromBody] ActionItem actionItem)
        {
            SuiviBE? developer = await _context.SuiviBEs
                                      .Include(a => a.ActionItems)
                                      .FirstOrDefaultAsync(d => d.SuiviBEId == id);

            if (developer == null)
            {
                return NotFound();
            }
            developer.ActionItems.Add(actionItem);
            await _context.SaveChangesAsync();

            return CreatedAtAction(nameof(Get), new { id = developer.SuiviBEId }, actionItem);

        }

How to fix the problem of the status 400? Thanks in advance to your help!

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

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-04-17T13:41:14.8833333+00:00

    How to fix the problem of the status 400?

    @sblb , you already know the 400 response is due to model validation on null entity framework navigation properties. Other than the working code I already provided in your many previous threads, the only other approach I can think of is trying the [JsonIgnore] attribute so the navigation properties are not serialized.

    [JsonIgnore] 
    public SuiviBE SuiviBE { get; set; 
    

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.