Status Code 200(ok) with 400(ok)

sblb 1,231 Reputation points
2023-10-05T16:27:20.0733333+00:00

Hi,

I've implemented handling concurrency errors which two users put the value in same moment;

When I invoke the put method in normal mode I get two threads, one with 400 statut code

User's image

In this case the statut code should only 200(ok).

below the put method and the razor page

async Task OnUpdateRow(ProjetModel newRow)
    {
        var response = await client.PutAsJsonAsync("api/projetbe", newRow);   

        if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
        {            
            ErrorResponseDictionary = new Dictionary<string, List<string>>();
            ErrorResponseDictionary = await response.Content.ReadFromJsonAsync<Dictionary<string, List<string>>>();
           
            uriHelper.NavigateTo("ppage");
            projetBEToUpdate = null;
            return;           
        }

        else
         {            
           await client.PutAsJsonAsync("api/projetbe", newRow);   
           uriHelper.NavigateTo("ppage");
           projetBEToUpdate = null;
           await OnInitializedAsync();
                
         } 
           
   }     

razor page


@if(ErrorResponseDictionary==null)
     { return; }
else if(ErrorResponseDictionary.Count() > 0)
{                          
   @foreach (KeyValuePair<string, List<string>> item in ErrorResponseDictionary)
       {      
       foreach (string message in ErrorResponseDictionary[item.Key])
          {
                                 <hr/>  
                                 <div>@message</div>
            }
        }
 }

Do you have any ideas? I'd also like to know if I can leave it or not?

Thanks in advance

Developer technologies | .NET | Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. sblb 1,231 Reputation points
    2023-10-07T20:22:12.2+00:00

    I'm not picking up on what you're saying...

    I've found the problem but I don't have the solution.

    As you know, I use the Radzen library;

    I use RadzenDataGrid in the inline editing mode, I based my code according to https://blazor.radzen.com/datagrid-inline-edit

    In this case I need to configure DataRadzenGrid as follows

    <RadzenDataGrid @ref="gridprojet" AEditMode="DataGridEditMode.Single"
                Data="@projet" TItem="ProjetModel" RowUpdate="@OnUpdateRow" …>
    
    ….
    </RadzenDataGridColumn>
      <EditTemplate Context="projet">
           <RadzenButton Icon="check" ButtonStyle="ButtonStyle.Success" 
             Variant="Variant.Flat" Size="ButtonSize.Medium" Click="@(args => 
              SaveRow(projet))">
                            
      </EditTemplate>
    </RadzenDataGridColumn>
    
    
    

    I've defined OnUpdateRow as below :

      async Task OnUpdateRow(ProjetModel newRow)
       {
         //  Reset();
           var response = await client.PutAsJsonAsync("api/projetbe", newRow);   
    
            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {            
                ErrorResponseDictionary = new Dictionary<string, List<string>>();
                ErrorResponseDictionary = await response.Content.ReadFromJsonAsync<Dictionary<string, List<string>>>();
    
                projetBEToUpdate = null;
                return;           
            }
            else
            {  
               projetBEToUpdate = null;
               return;
               await OnInitializedAsync();                
            }            
       }   
    

    I've defined a put method as below :

      [HttpPut]
            public async Task<IActionResult> Put(ProjetModel projetModel)
            {
                _context.Entry(projetModel).State = EntityState.Modified;
    
               try
                 {
                   await _context.SaveChangesAsync();
                 }
                 catch (DbUpdateConcurrencyException ex)
                  {
                        //throw;
                       var exceptionEntry = ex.Entries.Single();
                       var clientValues = (ProjetModel)exceptionEntry.Entity;
                       var databaseEntry = exceptionEntry.GetDatabaseValues();
                       var dbValues = (ProjetModel)databaseEntry.ToObject();
                       await SetDbErrorMessage(dbValues, clientValues);
    
                       projetModel.ConcurrencyToken = (byte[])dbValues.ConcurrencyToken;
                       ModelState.Remove($"{nameof(projetModel)}.{nameof(projetModel.ConcurrencyToken)}");
                  
                       return BadRequest(ModelState);
                  }
                return NoContent();
            }
    

    I've defined SaveRow as below

    async Task SaveRow(ProjetModel suivi)
        {
           await gridprojet.UpdateRow(suivi);   
        }
    

    In this configuration I obtain

    User's image

    But in this case, it's NOK because if the user makes two changes to the same page, he gets an error message and that's not what I want.

    User's image

    I think the problem lies in the configuration of inLine Mode of the RadzenDataGRid

    The UpdateRow in SaveRow is defined as below (from VS when reach the definition from RadzenDataGrid)

     // Résumé :
            //     Updates the row.
            //
            // Paramètres :
            //   item:
            //     The item.
            public async Task UpdateRow(TItem item)
            {
                if (!editedItems.Keys.Any((TItem i) => ItemEquals(i, item)))
                {
                    return;
                }
    
                if (editContexts[item].Validate())
                {
                    editedItems.Remove(item);
                    editContexts.Remove(item);
                    if (!object.Equals(itemToInsert, item))
                    {
                        await RowUpdate.InvokeAsync(item);
                    }
                    else
                    {
                        await RowCreate.InvokeAsync(item);
                        itemToInsert = default(TItem);
                    }
                }
    
                StateHasChanged();
            }
    

    You can see that the RowUpdate is invoked. This function is implemented is RadzenDataGRid

    If I want to implement Handling concurrency in RadzenDataGRid

    I have to call the put method twice to avoid receiving the message handling concurrency errors when the one user makes two modification in the same page.

    here's where I am!


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.