Iria
Iria

Reputation: 43

list of forms how to do this with Blazor?

I have a list of objects that needs to be updated,

    @foreach (var s in fooList)
    {
        <EditForm Model="@s" OnValidSubmit="@HandleValidSubmit">

            <tr>
                <td><InputText id="Name" @bind-Value="@s.Name" /></td>
                <td><InputText id="Description" @bind-value="s.Description" /></td>
                <td></td>
                <td>
                    <button type="submit">Submit</button>
                </td>
            </tr>
            </EditForm>
            }

This is razor.

private async Task HandleValidSubmit()
{
    await Service.UpdateService(s);
}

The problem that I have is that I get ' the name 's's does not exist in the current context' so , how do I pass the updated s in the foo list?

Upvotes: 0

Views: 459

Answers (1)

Remi THOMAS
Remi THOMAS

Reputation: 920

You need to derive from a common type, else .NET can't do the reflection correctly.

Take care about @bind-Value with capital V.

The code could be something like this

@page "/"

@if (fooList != null)
{
    @foreach (Common s in fooList)
    {
        <EditForm Model="s" OnValidSubmit="()=>HandleValidSubmit(s)">

            <tr>
                <td><InputText id="Name" @bind-Value="s.Name" /></td>
                <td><InputText id="Description" @bind-Value="s.Description" /></td>
                <td></td>
                <td>
                    <button type="submit">Submit</button>
                </td>
            </tr>
        </EditForm>
    }
}

@code
{
    public class Common
    {
        public string Name;
        public string Description;
    }

    public class Edit1 : Common
    {
        public string object1;
    }

    public class Edit2 : Common
    {
        public string object2;
    }


    List<Common> fooList = new List<Common>() { new Edit1() { Name = "t1" }, new Edit2() { Name = "t2" } };

    private Task HandleValidSubmit(Common result)
    {
        // Do something with result value
        return Task.CompletedTask;
    }
}

Upvotes: 1

Related Questions