【发布时间】:2022-01-25 13:34:21
【问题描述】:
我已将大组件分成两部分,其中一个是子组件,另一个是父组件。然而,目前在父组件值上单击提交按钮后,以某种方式变为空,并且没有数据传递到数据库中。为什么会这样以及如何解决这个问题?
在 Record.razor.cs 中
[Parameter]
public string Title { get; set; }
[Parameter]
public string Description { get; set; }
[Parameter]
public DateTime SelectedDate { get; set; }
Record.razor:
<Form Model="this" LabelColSpan="8" WrapperColSpan="16">
<FormItem Label="Record title" NoStyle>
<Input @bind-Value="@this.Title" />
</FormItem>
<FormItem Label="Description" NoStyle>
<Input @bind-Value="@this.Description" />
</FormItem>
<FormItem Label="Date" NoStyle>
<DatePicker @bind-Value="@this.SelectedDate" ShowTime="@true" OnChange="this.OnDateSelected" />
</FormItem>
</Form>
Index.razor.cs:
private DiaryRecord DiaryRecordModel { get; set; }
private bool isDialogVisible;
private DateTime SelectedDate { get; set; }
public Index()
{
this.DiaryRecordModel = new DiaryRecord();
this.SelectedDate = DateTime.Now;
}
Index.razor:
<Form Model="this.DiaryRecordModel"
OnFinish="(e) => this.OnCreateNewDiaryRecord()">
<FormItem>
<Record Description="@context.Description"
SelectedDate="this.SelectedDate"
Title="@context.Title" />
</FormItem>
<FormItem WrapperColOffset="8" WrapperColSpan="16">
<Button Type="@ButtonType.Primary" HtmlType="submit">
Submit
</Button>
<Button OnClick="(e)=>{this.isDialogVisible = false;}">Cancel</Button>
</FormItem>
</Form>
日记记录.cs
public class DiaryRecord
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
例如,今天的日期总是传递给模型,而不是实际选择的日期(其他值不带入模型):
【问题讨论】:
标签: c# blazor blazor-server-side blazor-webassembly