I have the following class hierarchy in my Blazor control.
class GrandChild
{
public string Description { get; set; } = "Grandchild Description";
}
class Child
{
public string Description { get; set; } = "Child Description";
public GrandChild GrandChild { get; set; } = new();
}
class Parent
{
public string Description { get; set; } = "Parent Description";
public Child Child { get; set; } = new();
};
partial class Index
{
Parent? Parent { get; set; } = new();
protected override void OnInitialized()
{
}
}
And my razor view looks like so:
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SfDataForm Model="@Parent">
<FormItems>
<FormItem Field="@nameof(Parent.Description)" LabelText="Parent Description" IsEnabled="false"> </FormItem>
<FormItem Field="@nameof(Parent.Child.Description)" LabelText="Child Description" IsEnabled="false"> </FormItem>
<FormItem Field="@nameof(Parent.Child.GrandChild.Description)" LabelText="Grandchild Description" IsEnabled="false"> </FormItem>
</FormItems>
</SfDataForm>
In the output it is showing only the Parent control description 3 times over instead of child and grandchild description
Update: If I change the code as follows:
class GrandChild
{
public string Description2 { get; set; } = "Grandchild Description";
}
class Child
{
public string Description1 { get; set; } = "Child Description";
public GrandChild GrandChild { get; set; } = new();
}
class Parent
{
public string Description { get; set; } = "Parent Description";
public Child Child { get; set; } = new();
};
And update Razor :
<SfDataForm Model="@Parent">
<FormGroup LabelText="Parent Information" ID="parent-information-group">
<FormItems>
<FormItem Field="@nameof(Parent.Description)" LabelText="Parent Description"> </FormItem>
<FormItem Field="@nameof(Parent.Child.Description1)" LabelText="Child Description"> </FormItem>
<FormItem Field="@nameof(Parent.Child.GrandChild.Description2)" LabelText="Grandchild Description"> </FormItem>
</FormItems>
</FormGroup>
</SfDataForm>
I now get an Exception:
Hi Chris Gilliam,
Thank you for reaching out to us.
We would like to inform you that to handle common properties efficiently across multiple classes, you can introduce a base class that encapsulates these shared properties. The other classes can then inherit from this base class, ensuring consistency and reusability in your application design.
To assist you, we have created a sample demonstrating this approach. You can explore it using the link below:
Sample:
https://blazorplayground.syncfusion.com/htVJWBjKhVeGCJJX
For more details on implementing validation and handling complex models, you can refer to our documentation:
Documentation Link : https://blazor.syncfusion.com/documentation/data-form/validation#complex-model-validation
If you need further assistance or clarification, please feel free to get back to us. We are happy to help you with any additional queries!
Regards,
Yohapuja S