我得到了一个不同的示例,其中包含一个必须序列化为表单模型的复杂参数对象。
如果参数发生变化,OnParametersSetAsync() 将更新子组件。
我们需要使用 @bind-Job="_job" 绑定子参数
家长:
<div class="tab-pane active" id="kt_job_tab_1">
<JobEdit @bind-Job="_job"></JobEdit>
</div>
@code
{
private Job _job;
protected override async Task OnInitializedAsync()
{
_job = await GetJob();
await base.OnInitializedAsync();
}
}
孩子:
<EditForm Model="@_model" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<div class="form-group row">
<label class="col-xl-3 col-lg-3 col-form-label text-sm-left text-lg-right">Title</label>
<div class="col-lg-9 col-xl-6">
<InputText @bind-Value="_model.JobTitle" class="form-control form-control-solid form-control-lg" />
</div>
</div>
<div class="form-group row">
<label class="col-xl-3 col-lg-3 col-form-label text-sm-left text-lg-right">Department</label>
<div class="col-lg-9 col-xl-6">
<InputText @bind-Value="_model.Department" class="form-control form-control-solid form-control-lg" />
</div>
</div>
</EditForm>
@code {
[Parameter]
public Job Job { get; set; }
[Parameter]
public EventCallback<Job> JobChanged { get; set; }
private Model _model;
protected override async Task OnInitializedAsync()
{
InitializeModel();
await base.OnInitializedAsync();
}
//THIS WILL UPDATE MODEL IF PARENT CHANGES
protected override async Task OnParametersSetAsync()
{
InitializeModel();
await base.OnParametersSetAsync();
}
private async void HandleValidSubmit()
{
//WE CODE HERE
//THEN
//THIS UPDATES PARENT IF CHILD JOB CHANGES
await JobChanged.InvokeAsync(Job);
StateHasChanged();
}
private void InitializeModel()
{
_model = new Model
{
JobIdentifier = Job.Identifier,
JobTitle = Job.JobTitle,
Department = Job.Department
};
}
private class Model
{
[Required]
public string JobIdentifier { get; set; }
[Required]
public string JobTitle { get; set; }
[Required]
public string Department { get; set; }
}
}