asp.net 将参数传递给OnValidSubmit方法

vuktfyat  于 2022-12-15  发布在  .NET
关注(0)|答案(1)|浏览(140)

你好,我正在用blazor做一些事情,我被困在传递值OnValidSubmit上。
这是我的全部组件:

@foreach(var post in Posts!){
 
<div class="col-md-12">
<div class="card">
            <div class="card-header">
               <div class="row">
                   <div class="col" style="text-align:center; font-weight:bold;">Post</div>
                       @* <div class="col" style="text-align:center; font-weight:bold;">Most popular comments</div>*@
               </div>
            </div>
<div class="row">
        <div class="col">
            <div class="card-body">
                <div class="row">
                    <div class="col">
                        <div class="card-text">
                        <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 class="col" style="padding:5px;">
                                         <div class="accordion accordion-flush" style="border:solid; "id="accordionFlushExample">
                                          <div class="accordion-item">
                                            <h2 class="accordion-header" id="flush-headingOne">
                                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-@post.Id" aria-expanded="false" aria-controls="flush-@post.Id">
                                              Newest comments
                                              </button>
                                            </h2>
                                        <div id="flush-@post.Id" class="accordion-collapse collapse" aria-labelledby="flush-headingOne" data-bs-parent="#accordionFlushExample">
                                              <div class="accordion-body">
                                                    @foreach (var comment in Comments!.Take(3))
                                                    {
                                                        <p><li class="list-group-item list-group-item-success">@comment.CommentDescription </li></p>
                                                    }                                                  </div>
                                            </div>
                                          </div>
                                        </div>
                            </div>
                </div>
                                <EditForm Model="@Comment" OnValidSubmit="@OnValidComment" OnInvalidSubmit="@OnInvalidComment">
                                    <DataAnnotationsValidator />
                                    <ValidationSummary />
                                    <div class="row">
                                        <div class="col-md-12">
                                        <p> <InputText type="text" class="form-control" placeholder="Your comment" id="comment" @bind-Value="@Comment.CommentDescription"></InputText></p>
                                            <ValidationMessage For="@(()=>Comment.CommentDescription)" />
                                        </div>

                                        <div class="col-md-2">
                                        <p> <InputText type="text" class="form-control" placeholder="Your nick" id="name" @bind-Value="@Comment.CommentAuthor"></InputText></p>
                                            <ValidationMessage For="@(()=>Comment.CommentAuthor)" />
                                            
                                        </div>
                                        <div class="col-md-3">
                                    <p><button type="submit" class="btn btn-success" name="id" value="@post.Id">Add comment</button></p>
                                        </div>
                                    </div>
                                </EditForm>
                        
                        <!-- Modal -->
                        <form method="get">
                            <div class="py-2">
                              <button type="button" data-bs-target="#Modal_@post.Id" name="Id" value="@post.Id" class="btn btn-primary" data-bs-toggle="modal">Show all comments</button>
                            </div>
                        </form>
                        <div class="modal fade" id="Modal_@post.Id" tabindex="-1" aria-hidden="true">

                            <div class="modal-dialog modal-xl">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="exampleModalLabel">Comments</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <div class="modal-body">
                                        @foreach(var comment in Comments!)
                                        {
                                         <hr><li class="list-group-item list-group-item-success"><b>Comment: </b>@comment.CommentDescription <b> <br>Author: </b>@comment.CommentAuthor <b>Creation Time: </b>@comment.CreationTime<br /></li>

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

                
</div>

</div>
</div>
}

我需要在此编辑表单中传递带有“添加评论”按钮的@post.Id

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

                                <div class="col-md-2">
                                <p> <InputText type="text" class="form-control" placeholder="Your nick" id="name" @bind-Value="@Comment.CommentAuthor"></InputText></p>
                                    <ValidationMessage For="@(()=>Comment.CommentAuthor)" />
                                    
                                </div>
                                <div class="col-md-3">
                            <p><button type="submit" class="btn btn-success" name="id" value="@post.Id">Add comment</button></p>
                                </div>
                            </div>
                        </EditForm>

所有酷,但我不能添加参数到@OnValidComment,所以我不能传递ID。
我得到:

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

我的目标是将@ post.id作为参数传递,这样我就可以将它传递给我的存储库。

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

uxhixvfz1#

当你想用lambda函数传递参数时,可以使用lambda函数:

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

相关问题