Blazor Button Onclickで引数をメンバ関数に渡す。
<button @onclick="(e=>ChangePropertyValue(引数1, 引数2, 引数))"></button>
@code {
private void ChangePropertyValue(int 引数1, string 引数2, string 引数)
{
Console.WriteLine($"{DateTime.Now}: ChangePropertyValueが呼び出されました。: {Value}");
StateContainer.Session_1 = 引数1;
StateContainer.Session_2 = 引数2;
StateContainer.Session_3 = 引数3;
}
}
ついでに、GETで取得したデータを、Foreachで表示。
ボタンがクリックされた場合は、値をSessionに渡す。
@page "/projectlist"
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject StateContainer StateContainer
@inject HttpClient Http;
<h3>ProjectList</h3>
<table>
<tr><td>name </td><td>go!</td>Title</tr>
@if (project_arrays != null)
{
foreach (var project in project_arrays)
{
<tr>
<td><a href = "/(行先)/@project.project_id.ToString()"> @project.project_title</a></td>
<td>x/x</td>
<td><button @onclick="(e=>ChangePropertyValue(project.project_id, project.project_title, project.project_dir))"></button></td>
</tr>
}
}
</table>
@code {
//private List<Project>? project_lists { get; set; } = new List<Project>();
//[Parameter]
//public int id { get; set; }
private int Value { get; set; }
private Project[] project_arrays;
protected override async Task OnInitializedAsync()
{
try
{
project_arrays = await Http.GetFromJsonAsync<Project[]>("api/project");
}
catch (AccessTokenNotAvailableException exception)
{
exception.Redirect();
}
}
protected override void OnInitialized()
{
Console.WriteLine($"{DateTime.Now}: projectlist::OnInitializedが呼び出されました。: {Value}");
//StateContainer.OnChange += StateHasChanged;
}
private void ChangePropertyValue(int _project_id, string _project_title, string _project_dir)
{
Console.WriteLine($"{DateTime.Now}: projectlist::ChangePropertyValueが呼び出されました。: {Value}");
StateContainer.Session_ProjectId = _project_id;
StateContainer.Session_ProjectTitle = _project_title;
StateContainer.Session_ProjectDir = _project_dir;
}
public void Dispose()
{
Console.WriteLine($"{DateTime.Now}: projectlist::Disposeが呼び出されました。: {Value}");
}
}