locked
what does [parameter] before method means? RRS feed

  • Question

  • User368830971 posted

    what does [parameter] before method means?

    Friday, May 29, 2020 3:35 AM

Answers

  • User711641945 posted

    Hi behzad.sdghi,

    Did you use blazor?

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

    Reference:

    https://docs.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>

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 5:45 AM
  • User303363814 posted

    If, when you say [parameter] you actually mean [Parameter]

    and when you say 'method' you mean 'property'

    then this page may help https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.1&viewFallbackFrom=aspnetcore-3.0#component-parameters

    If my assumptions are not correct can you show the code where this attribute is used?' 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 5:45 AM
  • User2078676645 posted

    Hi,

    I don't know where you use this parameter, if you use this parameter in blazor it means binding routing parameter or other in-page data, [parameter] is essential.

    Regards,

    Evern

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 5:47 AM

All replies

  • User-1955300613 posted

    The attribute before the method has many usages,they can provide a way of associating information with code in a declarative way. They can also provide a reusable element that can be applied to a variety of targets.

    What they mean depends on your method and what you want to do

    For example,[AcceptVerbs("GET", "POST")] can make the method support both get and post.

     [AcceptVerbs("GET", "POST")]
            public IActionResult VerifyEmail([RegularExpression("^[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$")] string email)
            {
                if (!ModelState.IsValid) 
                    return Json($"Don't use {email},change a form right now!");
               else
                    return Json(true);
    
            }

    or it can be used to get the parmeter from the models.

    [HttpGet("{id}")]
            public ActionResult<Pet> GetById(int id, bool dogsOnly)
            #endregion
            {
                var pet = _petsInMemoryStore.FirstOrDefault(
                    p => p.Id == id &&
                    (!dogsOnly || p.PetType == PetType.Dog));
    
                if (pet == null)
                {
                    return NotFound();
                }
    
                return pet;
            }

    you can search the official tutorial , they are useful,https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/attributes

    Friday, May 29, 2020 5:15 AM
  • User711641945 posted

    Hi behzad.sdghi,

    Did you use blazor?

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

    Reference:

    https://docs.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>

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 5:45 AM
  • User303363814 posted

    If, when you say [parameter] you actually mean [Parameter]

    and when you say 'method' you mean 'property'

    then this page may help https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.1&viewFallbackFrom=aspnetcore-3.0#component-parameters

    If my assumptions are not correct can you show the code where this attribute is used?' 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 5:45 AM
  • User2078676645 posted

    Hi,

    I don't know where you use this parameter, if you use this parameter in blazor it means binding routing parameter or other in-page data, [parameter] is essential.

    Regards,

    Evern

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 29, 2020 5:47 AM