I am just adding data from blazor to api. I check the data is bound and not empty but when It arrives the controller it always null. this is just easy api request.
I am just adding data from blazor to api. I check the data is bound and not empty but when It arrives the controller it always null. this is just easy api request.
[HttpPost]
public async Task<ActionResult> AddRole(string roleName)
{
if (roleName != null)
{
await _roleManager.CreateAsync(new IdentityRole(roleName.Trim()));
}
return Ok();
}
Blazor code
@page "/role"
@inject HttpClient client
@inject IJSRuntime js
<h3>Roles</h3>
<small>Add as many role as you wish.</small>
<div class="form-group">
<input @bind:event="oninput" @bind="roleName" />
<a>@roleName</a>
</div>
<button type="submit" @onclick="AddNewRole" class="btn btn-success">
Submit
</button>
<br>
@if (roles == null)
{
<text>Loading...</text>
}
else if (roles.Length == 0)
{
<text>No Records Found.</text>
}
else
{
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Role Name</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (RoleDto role in roles)
{
<tr>
<td>@role.Id</td>
<td>@role.Name</td>
<td>
<button class="btn btn-danger" @onclick="@(() => ManagePermission(role.Id))">Manage Role</button>
<a class="btn btn-success" href="role/edit/@role.Id">Edit</a>
<button class="btn btn-danger" @onclick="@(() => Delete(role.Id))">Delete</button>
</td>
</tr>
}
</tbody>
</table>
}
@code {
RoleDto[] roles { get; set; }
string roleName { get; set; }
protected override async Task OnInitializedAsync()
{
roles = await client.GetFromJsonAsync<RoleDto[]>("api/roles");
}
async Task AddNewRole()
{
await client.PostAsJsonAsync("api/roles", roleName);
await OnInitializedAsync();
}
}