In Blazor, I have an EditForm where the user can specify the details of a new task, such as the owner of the task. The tasks and clients are separate classes, that are stored in their own data table. Now, on the edit form where the user specifies a new task, in the client field, I want them to be able to search for an existing client, and, if the client doesn't exist yet, create a new one.
So I've added a button in the tasks edit form that opens a new edit form where the client details can be added. And on saving those client details, I want the user to be redirected back to the tasks edit form, so they can be saved as well.
My problem is, on redirecting to the task edit form, that page loses all data, and doesn't reinitialize the form correctly. If I don't do anything, I get an error that the parameters aren't initialized, and if I manually re-initialize the parameters, I'm losing all data, and the button to save the task form stops working. Isn't there any way to do this?
This is what my code looks like:
<EditForm Model="@task" OnValidSubmit="@OnValidSubmit">
<DataAnnotationsValidator />
<div class="form-group">
<label>Description :</label>
<div>
<InputText @bind-Value="@task.Description" class="form-control col-sm-3" />
<ValidationMessage For="@(() => task.Description)" />
</div>
</div>
<div>
<label>Client :</label>
<div>
<SfAutoComplete TValue="Client" TItem="Client" Placeholder="Specify client name" AllowCustom=true @bind-Value="@task.Client" DataSource="@clients">
<AutoCompleteFieldSettings Value="Name" />
</SfAutoComplete>
</div>
</div>
<div class ="form-group">
<a class ="btn btn-success" href="/createclient" ><i class = "oi oi-plus"></i> Create New</a>
</div>
<button type="submit" class="btn btn-success">
@ButtonText
</button>
</EditForm>
@code {
[Parameter]
public CreateJobRequest task { get; set; }
[Parameter]
public string ButtonText { get; set; } = "Save";
[Parameter]
public EventCallback OnValidSubmit { get; set; }
private IEnumerable<Api.Entities.Client>? clients;
SfAutoComplete<string, Client> AutocompleteObj { get; set; }
protected override async Task OnInitializedAsync()
{
clients = await ClientService.GetAll();
}
private async Task OnFilter(FilteringEventArgs args)
{
args.PreventDefaultAction = true;
var query = new Query().Where(new WhereFilter() { Field = "Name", Operator = "contains", value = args.Text, IgnoreCase = true });
query = !string.IsNullOrEmpty(args.Text) ? query : new Query();
await AutocompleteObj.FilterAsync(clients, query);
var t = AutocompleteObj.GetItemsAsync();
}
}
I've tried re-initializing the parameters with
@if(task == null)
{
task = new CreateJobRequest();
ButtonText = "Save";
OnValidSubmit = new EventCallback();
}
but besides then losing all data in 'task', the button to save the task form still doesn't work anymore.
Can anyone help me with this? Or is there a way to enter the client details directly in the task edit form?
Since you did not say if you are using Blazor Server of Blazor WASM with or without Entity Framework Core, for now I've assumed you're using Blazor Server with EF Core. The code below should do just fine:
Models:
CTask.csClient.csAddTask.razor
Note: I don't have Syncfusion. Just replace the InputSelect with your AutoComplete
AddTask.razor.cs
TaskBL.cs
TaskRepos.cs
When an existing
Clientis selected, nothing happens with theclient table. When the user added a newClientEF Core will add this to theclient tableand return the Id to theCTask