Created
October 12, 2020 23:09
-
-
Save JIOO-phoeNIX/66c9b96e2b5ada88876f13e99e4e64ee to your computer and use it in GitHub Desktop.
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 "/CreateAccount" | |
@using BlazorAppWithCockroachDB.Model; | |
@using Newtonsoft.Json | |
@inject NavigationManager NavigationManager | |
@inject HttpClient Http | |
<h3>Create an Account</h3> | |
<div class="row"> | |
<EditForm Model="@accountModel" OnValidSubmit="@InsertAccount"> | |
<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">Submit</button> | |
</EditForm> | |
</div> | |
<br /> | |
<br /> | |
<a href="/Accounts">Back to List</a> | |
@code { | |
private AccountModel accountModel = new AccountModel(); | |
private async Task InsertAccount() | |
{ | |
int lastAccountId = await Http.GetFromJsonAsync<int>("Account/GetLastAccountId"); | |
int newAccountId = lastAccountId + 1; | |
Account account = new Account | |
{ | |
id = newAccountId, | |
balance = accountModel.balance, | |
name = accountModel.name | |
}; | |
var response = await Http.PostAsJsonAsync("Account/Create", account); | |
if (response.IsSuccessStatusCode) | |
{ | |
var responseText = await response.Content.ReadAsStringAsync(); | |
Account mewAccount = JsonConvert.DeserializeObject<Account>(responseText); | |
NavigationManager.NavigateTo($"EditAccount/{mewAccount.id}"); | |
} | |
else | |
{ | |
NavigationManager.NavigateTo("CreateAccount"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment