# Adding fields inside a Template Form dynamically

**URL:** <https://forum.radzen.com/t/adding-fields-inside-a-template-form-dynamically/14295>\
**Category:** Radzen.Blazor Components\
**Created:** [June 6, 2023, 8:07am UTC](https://forum.radzen.com/t/adding-fields-inside-a-template-form-dynamically/14295 "2023-06-06T08:07:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![copemed](https://forum.radzen.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@copemed](https://forum.radzen.com/u/copemed)\
**Post date:** [June 6, 2023, 8:07am UTC](https://forum.radzen.com/t/adding-fields-inside-a-template-form-dynamically/14295/1 "2023-06-06T08:07:10Z")

</div>

Is it possible to create entry fields inside a template form dynamically via a button press.

**Example:**  
Alpha is a class containing ` List<string> betas`  
Beta is an element of ` List<string> betas`

 ![Dynamic form fields](https://forum.radzen.com/uploads/default/original/2X/f/ff9a8c1f86f4f814e7270f58bfbedb607486e1a7.jpeg)

This works correctly when not contained inside a Template Form, but when I try to implement it using a Template Form it throws this error.

 ![image](https://forum.radzen.com/uploads/default/original/2X/b/b0a363f2a2683a7f336c0449dbd34343e70de9db.png)

The reason I'm trying to use a Template Form is because I want to implement validation for each entry field.

Any advice would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [June 6, 2023, 8:59am UTC](https://forum.radzen.com/t/adding-fields-inside-a-template-form-dynamically/14295/2 "2023-06-06T08:59:31Z")

</div>

Hi @copemed,

Yes, you can use a foreach and a collection to create the fields. Here is another thread that shows something similar: [RadzenTemplateForm can't submit](http://forum.radzen.com/t/radzentemplateform-cant-submit/3681)

---

<div class="post-metadata">

**Author:** ![copemed](https://forum.radzen.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@copemed](https://forum.radzen.com/u/copemed)\
**Post date:** [June 6, 2023, 9:16am UTC](https://forum.radzen.com/t/adding-fields-inside-a-template-form-dynamically/14295/3 "2023-06-06T09:16:27Z")

</div>

My apologies I didn't fully explain my problem.

I have a used a foreach and a collection `Alpha.betas` to create the fields, which works correctly when adding new fields outside of a Template Form; however when I try to contain the form within a Template Form and try to add a new field it throws the error shown above.

**Add button:** `Click ="@AddField"`

```auto
private void AddField() {
newAlpha.betas.Add("");
}

```

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [June 6, 2023, 9:45am UTC](https://forum.radzen.com/t/adding-fields-inside-a-template-form-dynamically/14295/4 "2023-06-06T09:45:05Z")

</div>

This exception is thrown by the Blazor framework. It doesn't support array indexing for `@bind` expressions. The workaround is to use Value and ValueChanged instead:

```auto
<RadzenTextBox Value="@items[index].Value"
               ValueChanged="@(value => items[index].Value = value)" />

```

---

<div class="post-metadata">

**Author:** ![copemed](https://forum.radzen.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@copemed](https://forum.radzen.com/u/copemed)\
**Post date:** [June 6, 2023, 9:55am UTC](https://forum.radzen.com/t/adding-fields-inside-a-template-form-dynamically/14295/5 "2023-06-06T09:55:39Z")

</div>

Thank you it works perfectly
