Search code examples
blazorblazor-component

Blazor templated component to render a form with different context


I have a serie of textboxes to get phone numbers and emails from the user, and I am going to use them in many forms but with different context, for example client, employee, supplier, so I like to know if it is possible to create a templated component to reuse the code.

<div class="form-group row">
    <label for="phone1" class="col-sm-3">Phone 1: </label>
    <InputText id="phone1" @bind-Value="@Client.phone1"></InputText>
</div>

How Can I change the context and call the component like this

<Phones TValue=Employee />
<Phones TValue=Client />
<Phones TValue=Supplier />

All this classes have the same phone and email properties.

Thank you


Solution

  • I'm a little unclear why you want to do this. If "Employee," "Client" and "Supplier" are all derived from the same base-class (which they very much should be), then you don't need to declare a TValue in the markup at all. Just have a [Parameter] public BasePersonModel Person {get; set; }

    Then in your component's markup, you can use whatever logic you want on the Person object, for example:

    @if (Person is Employee) { Do something }