how to refresh the page after submit event (POST) in Razor Pages

sblb 1,166 Reputation points
2023-08-26T14:21:49.6466667+00:00

Hi,

I create an action via the post method in modal dialog.

When I run my app and Create an action the post method works fine.

When I want to create a new action the field in modal is not empty and keep the last action data.

However I use : async Task

Below the code in razor page :

  [Parameter] public EventCallback OnSubmitCallback { get; set; }

 async Task CreationAction(int actionId)
    {    
            
      var result = await DialogService.OpenAsync("Creation de l'action", ds =>
       @<div> 
...
      <RadzenButton Text="Update" Icon="report" ButtonStyle="ButtonStyle.Light" Click=@CreateActionList />              

        async Task CreateActionList()
        {
             
            await client.PostAsJsonAsync($"api/etude/{actionId}", act); 
            uriHelper.NavigateTo($"etude/actions/{dev.SuiviBEId}");
           
            await OnSubmitCallback.InvokeAsync();
        }         
    }

Have you an idea how to avoid it?

Thanks in advance!

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,379 questions
{count} votes

Accepted answer
  1. Tiny Wang-MSFT 1,571 Reputation points Microsoft Vendor
    2023-08-28T06:30:35.5233333+00:00

    Hi @sblb , do you mean that, you want to init all the fields after submitting the data? Normally speaking, we always use model binding in blazor to control the data and view. So I have sample like this and this is my test result.

    @page "/counter"
    @using BlazorApp1.Data;
    
    <PageTitle>Counter</PageTitle>
    
    <h1>Counter</h1>
    
    <p role="status">Current count: @currentCount</p>
    
    <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
    
    <EditForm Model="@starship" OnValidSubmit="@HandleValidSubmit">
        <DataAnnotationsValidator />
        <ValidationSummary />
    
        <p>
            <label>
                Identifier:
                <InputText @bind-Value="starship.Identifier" />
            </label>
        </p>
        <p>
            <label>
                Description (optional):
                <InputTextArea @bind-Value="starship.Description" />
            </label>
        </p>
        <p>
            <label>
                Primary Classification:
                <InputSelect @bind-Value="starship.Classification">
                    <option value="">Select classification ...</option>
                    <option value="Exploration">Exploration</option>
                    <option value="Diplomacy">Diplomacy</option>
                    <option value="Defense">Defense</option>
                </InputSelect>
            </label>
        </p>
        <p>
            <label>
                Maximum Accommodation:
                <InputNumber @bind-Value="starship.MaximumAccommodation" />
            </label>
        </p>
        <p>
            <label>
                Engineering Approval:
                <InputCheckbox @bind-Value="starship.IsValidatedDesign" />
            </label>
        </p>
        <p>
            <label>
                Production Date:
                <InputDate @bind-Value="starship.ProductionDate" />
            </label>
        </p>
    
        <button type="submit">Submit</button>
    
        <p>
            <a href="http://www.startrek.com/">Star Trek</a>,
            ©1966-2019 CBS Studios, Inc. and
            <a href="https://www.paramount.com">Paramount Pictures</a>
        </p>
    </EditForm>
    
    @code {
        private int currentCount = 0;
        private Starship starship = new() { ProductionDate = DateTime.UtcNow };
    
        private void HandleValidSubmit()
        {
            var a = "1";
            starship = new() { ProductionDate = DateTime.UtcNow };
            //Logger.LogInformation("HandleValidSubmit called");
    
            // Process the valid form
        }
        private void IncrementCount()
        {
            currentCount++;
        }
    }
    

    Animation6

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful