Created
October 12, 2020 23:24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/EditAccount/{Id:int}" | |
@using BlazorAppWithCockroachDB.Model; | |
@using Newtonsoft.Json | |
@inject NavigationManager NavigationManager | |
@inject HttpClient Http | |
<h3>Edit Account</h3> | |
<div class="row"> | |
<EditForm Model="@accountModel" OnValidSubmit="@UpdateAccount"> | |
<DataAnnotationsValidator /> | |
<ValidationSummary /> | |
<div class="form-group"> | |
Balance: <InputNumber id="balance" @bind-Value="accountModel.balance" class="form-control" /> | |
</div> | |
<div class="form-group"> | |
Name: <InputText id="name" @bind-Value="accountModel.name" class="form-control" /> | |
</div> | |
<br /> | |
<button type="submit" class="btn btn-success">Update</button> | |
</EditForm> | |
</div> | |
<br /> | |
<br /> | |
<a href="/Accounts">Back to List</a> | |
@code { | |
[Parameter] | |
public int? Id { get; set; } | |
private AccountModel accountModel = new AccountModel(); | |
protected override async Task OnInitializedAsync() | |
{ | |
if (Id == null) | |
{ | |
NavigationManager.NavigateTo("Accounts"); | |
} | |
var response = await Http.GetAsync($"Account/Get/{Id}"); | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseText = await response.Content.ReadAsStringAsync(); | |
Account account = JsonConvert.DeserializeObject<Account>(responseText); | |
accountModel.name = account.name; | |
accountModel.balance = account.balance; | |
} | |
else | |
{ | |
NavigationManager.NavigateTo("Accounts"); | |
} | |
} | |
private async Task UpdateAccount() | |
{ | |
Account account = new Account | |
{ | |
id = Id.Value, | |
balance = accountModel.balance, | |
name = accountModel.name | |
}; | |
await Http.PostAsJsonAsync("Account/Update", account); | |
NavigationManager.NavigateTo("Accounts"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment