behzad sadeghi
behzad sadeghi

Reputation: 25

what does parameter before method means in asp.net core?

I am using asp.net core 3, in a video tutorial I see use [parameter]. I want to know whats its name and whats its application?

what does [parameter] before method means?

 [parameter] 
 public int incrementgap{ get; set; } = 1;

    void inccount()

    { currentCount+=incrementgap; }

Upvotes: 0

Views: 111

Answers (1)

Rena
Rena

Reputation: 36595

[Parameter] is used to mark the component parameters that can be set when the component is used in another page.

Reference:

https://learn.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.1#component-parameters

For more detailed exlpanation,in this code below:

<div class="panel panel-default">
    <div class="panel-heading">@Title</div>
    <div class="panel-body">@ChildContent</div>

    <button class="btn btn-primary" @onclick="OnClick">
        Trigger a Parent component method
    </button>
</div>

@code {
    public string Title { get; set; }

    public RenderFragment ChildContent { get; set; }

    public EventCallback<MouseEventArgs> OnClick { get; set; }
}

If you do not add [Parameter] attribute, those are just public properties that can't be set from other pages.That is to say the following line would be invalid :

<ChildComponent Title="Panel Title from Parent" />

The correct way should be:

<div class="panel panel-default">
    <div class="panel-heading">@Title</div>
    <div class="panel-body">@ChildContent</div>

    <button class="btn btn-primary" @onclick="OnClickCallback">
        Trigger a Parent component method
    </button>
</div>

@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> OnClickCallback { get; set; }
}

Then it allows you could set the parameters whenever we use that component:

<ChildComponent Title="Panel Title from Parent"
                OnClickCallback="@ShowMessage">
    Content of the child component is supplied
    by the parent component.
</ChildComponent>

Upvotes: 1

Related Questions