Reputation: 141
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 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="#[email protected]" aria-expanded="false" aria-controls="[email protected]">
Newest comments
</button>
</h2>
<div id="[email protected]" 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="#[email protected]" name="Id" value="@post.Id" class="btn btn-primary" data-bs-toggle="modal">Show all comments</button>
</div>
</form>
<div class="modal fade" id="[email protected]" 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>
}
I need to pass @post.Id with Add comment button in this EditForm
<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>
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();
}
Upvotes: 1
Views: 1107
Reputation: 1365
Use a lambda function when you want to pass an argument with the function:
OnValidSubmit="@(() => OnValidComment(post.Id))"
Upvotes: 1