3

I am learning about EventCallBack with blazor, but i wanted to know

  1. Is there a way for my return method to return a second argument on the parent side. I would like to know the rowID that it came from

Parent Code

 @foreach (var rw in pg.rows)
    {
        @if (rw.rowDef == 1)
        {
            @if (rw.widgetsSection1.Count == 0)
            {
                <AddWidgetToColumnComponent rowID="rw.rowID" onAddingWidget="AddWidgetToRow"></AddWidgetToColumnComponent>
            }
        }
    }
}

@code{
    ...
    private void AddWidgetToRow(GenericWidgets evnt)
    {
        //bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure?");
        var row = pg.rows.Where(x => x.rowID == 3).FirstOrDefault();
        //row.addWidgetSection1(widgets);
    }
}

Child Component AddWidgetToColumnComponent

<button class="btn btn-info" @onclick="@addComponent">Ajouter</button>

@code {
    [Parameter]
    public int rowID { get; set; }

    [Parameter]
    public EventCallback<GenericWidgets> onAddingWidget { get; set; }

    public async void addComponent()
    {
        GenericWidgets widget = new GenericWidgets();
        await onAddingWidget.InvokeAsync(widget);
    }

I am not sure how to accomplish this, if possible (debugger give me error). The only other way would be to change my generic class to add the rowID attribute in it. But is there an other way?

Change the lines ( what i would like it to be)

<AddWidgetToColumnComponent rowID="rw.rowID" onAddingWidget="AddWidgetToRow(rw.rowID)"></AddWidgetToColumnComponent>

and this line

private void AddWidgetToRow(GenericWidgets evnt, int rowID)
2
  • 1
    This question is a bit hard to understand. For question 1, are you asking about debugging or getting the callback to fire on the parent? For question 2, are you asking whether it's possible for AddWidgetToRow to receive additional parameters? You mentioned at the end that you changed some lines, and this is also a bit confusing. Which version should I be trying to understand? You will have better luck getting answers if you make this as clear as possible. :) Commented Nov 23, 2021 at 19:46
  • 1
    For part 1, to fire on the parent i would say, The lines I mention, is the entention i intent to do, however it fails. The line at the end is what i would like it to do. If it make any sense Commented Nov 23, 2021 at 19:47

1 Answer 1

3

If the question is about how to get an EventCallback to fire on the parent with multiple parameters, I usually achieve this by creating a class to encapsulate everything that needs to get passed.

So you might end up with something like:

 [Parameter] public EventCallback<AddWidgetArgs> onAddingWidget { get; set; }

And AddWidgetArgs is a class, like:

public class AddWidgetArgs 
{
    public GenericWidgets GenericWidget { get; set; } // My guess at your 1st parameter 
    public int RowId { get; set; } // Additional parameter
}

Then you consume it on the parent like this:

 private void AddWidgetToRow(AddWidgetArgs args)  { ... } // Or async Task, etc.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.