Skip to content

Instantly share code, notes, and snippets.

@JIOO-phoeNIX
Created October 12, 2020 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JIOO-phoeNIX/66c9b96e2b5ada88876f13e99e4e64ee to your computer and use it in GitHub Desktop.
Save JIOO-phoeNIX/66c9b96e2b5ada88876f13e99e4e64ee to your computer and use it in GitHub Desktop.
@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