How to Pass Parameters in Blazor

Yusuf-3141 50 Reputation points
2024-12-07T10:00:54.7866667+00:00

Hi,
how can I use something like CommandParameter in Xamarin, but in Blazor
for example:

<div x-index="3" y-index="2"/>

Then access them programmaticaly in C#.

Thanks in advance

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,636 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Yusuf-3141 50 Reputation points
    2024-12-11T11:29:19.3966667+00:00

    I found a solution using Blazor components

    1 person found this answer helpful.
    0 comments No comments

  2. AgaveJoe 28,961 Reputation points
    2024-12-07T12:21:02.62+00:00

    I think Blazor data binding is what you need to learn.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-9.0

    Using your example markup, the x-index and y-index are assigned to C# expressions. Updating the variables affects the markup attributes.

    @page "/parameterex"
    @rendermode InteractiveWebAssembly
    <h3>ParameterEx</h3>
    <div x-index="@xIndex" y-index="@yIndex" />
    <button @onclick="UpdateParamters">Update</button>
    @code {
        private int xIndex = 3;
        private int yIndex = 2;
        private void UpdateParamters()
        {
            xIndex = xIndex * 2;
            yIndex = yIndex * 2;
        }
    }
    
    0 comments No comments

  3. Bruce (SqlWork.com) 68,486 Reputation points
    2024-12-09T16:53:29.57+00:00

    in Blazor you use a lambda expression to pass the parameter(s) by closure.

    <button @onclick="() => MyCallback(myparam1)">Click me</button>
    

    note: the click event is captured by javascript and sent to the Blazor app via jsinterop, so no additional parameters are supported at the event level.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.