Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to bind a Blazor form to an enumerable

$
0
0

I'm trying to provide an empty set of text boxes to a user, get their input, and send the result to an API. Ideally this would not be a fixed size list, but I can live with that for now. How can I get it to bind succesfully? My form looks like this:

public class PlanForm(){    public List<Day> Days { get; set; }}public class Day{    public string Tag { get; set; }}

and I've tried both for and foreach loops, here is the foreach:

<EditForm Model="PlanForm" OnValidSubmit="Submit" FormName="Plan">    @foreach (var day in PlanForm!.Days)    {<div><label><InputText @bind-Value="day.Tag" /></label></div>    }<div><button type="submit">Submit</button></div></EditForm>

I'm initialising like this:

protected override void OnInitialized() =>    PlanForm ??= new PlanForm        {            Days = new List<Day> { new Day{ Tag=""}, new Day{Tag=""}        }    };

and when I submit and put in a breakpoint, PlanForm.Days has just the empty strings as its tags, instead of what was submitted thorough the browser.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>