Home > database >  Passing argument to OnValidSubmit method
Passing argument to OnValidSubmit method

Time:12-06

Hello i am working on something with blazor and i am stuck at passing value OnValidSubmit.

This is my whole component:

@foreach(var post in Posts!){
 
<div >
<div >
            <div >
               <div >
                   <div  style="text-align:center; font-weight:bold;">Post</div>
                       @* <div  style="text-align:center; font-weight:bold;">Most popular comments</div>*@
               </div>
            </div>
<div >
        <div >
            <div >
                <div >
                    <div >
                        <div >
                        <p><b>Title: </b> @post.Title</p>
                        <p> <b>Description: </b> @post.Description</p>
                        <p> <b>Nick: </b> @post.Author</p>
                        <p> <b>Category: </b> @post.Category</p>
                        <p> <b>Created: </b> @post.CreationTime</p>
                      </div>
                    </div>
                            <div  style="padding:5px;">
                                         <div  style="border:solid; "id="accordionFlushExample">
                                          <div >
                                            <h2  id="flush-headingOne">
                                            <button  type="button" data-bs-toggle="collapse" data-bs-target="#[email protected]" aria-expanded="false" aria-controls="[email protected]">
                                              Newest comments
                                              </button>
                                            </h2>
                                        <div id="[email protected]"  aria-labelledby="flush-headingOne" data-bs-parent="#accordionFlushExample">
                                              <div >
                                                    @foreach (var comment in Comments!.Take(3))
                                                    {
                                                        <p><li >@comment.CommentDescription </li></p>
                                                    }                                                  </div>
                                            </div>
                                          </div>
                                        </div>
                            </div>
                </div>
                                <EditForm Model="@Comment" OnValidSubmit="@OnValidComment" OnInvalidSubmit="@OnInvalidComment">
                                    <DataAnnotationsValidator />
                                    <ValidationSummary />
                                    <div >
                                        <div >
                                        <p> <InputText type="text"  placeholder="Your comment" id="comment" @bind-Value="@Comment.CommentDescription"></InputText></p>
                                            <ValidationMessage For="@(()=>Comment.CommentDescription)" />
                                        </div>

                                        <div >
                                        <p> <InputText type="text"  placeholder="Your nick" id="name" @bind-Value="@Comment.CommentAuthor"></InputText></p>
                                            <ValidationMessage For="@(()=>Comment.CommentAuthor)" />
                                            
                                        </div>
                                        <div >
                                    <p><button type="submit"  name="id" value="@post.Id">Add comment</button></p>
                                        </div>
                                    </div>
                                </EditForm>
                        
                        <!-- Modal -->
                        <form method="get">
                            <div >
                              <button type="button" data-bs-target="#[email protected]" name="Id" value="@post.Id"  data-bs-toggle="modal">Show all comments</button>
                            </div>
                        </form>
                        <div  id="[email protected]" tabindex="-1" aria-hidden="true">

                            <div >
                                <div >
                                    <div >
                                        <h5  id="exampleModalLabel">Comments</h5>
                                        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <div >
                                        @foreach(var comment in Comments!)
                                        {
                                         <hr><li ><b>Comment: </b>@comment.CommentDescription <b> <br>Author: </b>@comment.CommentAuthor <b>Creation Time: </b>@comment.CreationTime<br /></li>

                                        }
                                    </div>
                                    <div >
                                        <button type="button"  data-bs-dismiss="modal">Close</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- Modal -->
            </div>
        </div>

                
</div>

</div>
</div>
}

I need to pass @post.Id with Add comment button in this EditForm

                        <EditForm Model="@Comment" OnValidSubmit="@OnValidComment" OnInvalidSubmit="@OnInvalidComment">
                            <DataAnnotationsValidator />
                            <ValidationSummary />
                            <div >
                                <div >
                                <p> <InputText type="text"  placeholder="Your comment" id="comment" @bind-Value="@Comment.CommentDescription"></InputText></p>
                                    <ValidationMessage For="@(()=>Comment.CommentDescription)" />
                                </div>

                                <div >
                                <p> <InputText type="text"  placeholder="Your nick" id="name" @bind-Value="@Comment.CommentAuthor"></InputText></p>
                                    <ValidationMessage For="@(()=>Comment.CommentAuthor)" />
                                    
                                </div>
                                <div >
                            <p><button type="submit"  name="id" value="@post.Id">Add comment</button></p>
                                </div>
                            </div>
                        </EditForm>

All cool but i cannot add parameters to @OnValidComment so i cannot pass Id.

I get:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  CS1503  Argument 2: cannot convert from 'method group' to 'EventCallback'       a:\Users\sam_sepi0l\Desktop\PROJECTS\NEWFORUM\TORCHAIN\Components\PostView.razor    43  

My goal is to pass @post.Id as parameter so i can pass it to my repository.

private async Task OnValidComment(int id)
{
   await _repository!.AddComment(Comment.CommentDescription,Comment.CommentAuthor,Comment.Id,false,DateTime.Now);
   Comments = await _repository!.GetAllComments();
}

CodePudding user response:

Use a lambda function when you want to pass an argument with the function:

OnValidSubmit="@(() => OnValidComment(post.Id))"
  • Related