在OnValidSubmit()过程中,ICollection属性为空



我正在尝试Syncfusion Blazor SfGrid的免费社区版本,用于ICollection属性的基本CRUD。但是,虽然我可以在运行时添加、编辑、删除(数据在数据网格中完美显示(,但在OnValidSubmit()期间,我的ICollection属性始终为空。。。

第一:我的数据模型如下:

public class Contact
{
public int ID { get; set; }
public string AccountName { get; set; }
public ICollection<ContactPerson> ContactPersons { get; set; }
}
public class ContactPerson
{
public int ID { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}

第二:我的create.razor使用我的ContactForm.razor进行可重用

<h1>Create</h1>
<hr />
<div style="margin: 50px">
<ContactForm Contact="contact" OnValidSubmit="@CreateRecord" OnCancelButton="@CancelRecord"/>
</div>
@code {
private Contact contact = new Contact
{
ContactPersons = new ContactPerson[0],
ContactAddresses = new ContactAddress[0]
};
void CancelRecord()
{
uriHelper.NavigateTo("contact");
}
async Task CreateRecord()
{          
await http.PostAsJsonAsync("api/Contact", contact);
uriHelper.NavigateTo("contact");
}
}

第三:我的ContactForm.razor看起来像这样(我用来创建和编辑记录(:

@if (Contact == null)
{
<text>Loading...</text>
}
else
{
<EditForm Model="@Contact" OnValidSubmit="@OnValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-row">
<div class="form-group col-md-3">
<SfTextBox @bind-Value="@Contact.AccountName" Placeholder="Account Name"></SfTextBox>
<ValidationMessage For="@(() => Contact.AccountName)"></ValidationMessage>
</div>
</div>

<!-- List of Contact Persons -->
<div class="form-row">
<div class="form-group col-md-10">
<SfGrid DataSource="@Contact.ContactPersons" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })" AllowPaging="true">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(ContactPerson.ID) IsPrimaryKey="true" HeaderText="ID"></GridColumn>
<GridColumn Field=@nameof(ContactPerson.FirstName) HeaderText="First Name"></GridColumn>
<GridColumn Field=@nameof(ContactPerson.MiddleName) HeaderText="Middle Name"></GridColumn>
<GridColumn Field=@nameof(ContactPerson.LastName) HeaderText="Last Name"></GridColumn>                        
</GridColumns>
</SfGrid>
</div>
</div>
<!-- Post and Back Link -->
<hr />
<div>
<SfButton type="submit" CssClass="e-primary">Sumbit</SfButton>
<SfButton type="button" CssClass="e-danger" @onclick="@OnCancelButton">Cancel</SfButton>
</div>
</EditForm>
}
@code {
[Parameter] public Contact Contact { get; set; }
[Parameter] public EventCallback OnValidSubmit { get; set; }
[Parameter] public EventCallback OnCancelButton { get; set; }
}

我遇到的问题是,当我将Contact.ContactPersons传递给我的控制器(webapi(时,它是空的,当我在CreateRecord() which is `` OnValidSubmit处中断并在VS2019中观察变量Contact.ContactPersons时,该变量是空的并显示

无法评估。

请帮我了解发生了什么。

与Blazor无关的问题。。。

由于ContactPersons属于ICollection<ContactPerson>类型,因此应该使用ContactPerson对象的集合实例化ContactPerson字段,如下所示:

private Contact contact = new Contact
{
ContactPersons = new List<ContactPerson>()

};

相关内容

  • 没有找到相关文章

最新更新