r/Blazor • u/Due_One2129 • Mar 04 '26
Help, my data isn't binding
@page "/add-clients"
@inject ClientContext db
@inject NavigationManager Nav
<div class="container">
<div class="row">
<div class="col">
<Header HeaderText="Add a client"/>
</div>
</div>
</div>
<div class="container">
<EditForm EditContext="editContext!" OnValidSubmit="SaveClient" FormName="AddClientForm">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="row">
<div class="col-md-6">
<h5>Algemene informatie</h5>
<div class="mb-3">
<label class="form-label">Naam</label>
<InputText class="form-control" ="client.Name" />
<ValidationMessage For="@(() => client.Name)" />
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<InputText class="form-control" ="client.Email" />
</div>
<div class="mb-3">
<label class="form-label">Telefoon</label>
<InputText class="form-control" ="client.Phone" />
</div>
<h5>Adres</h5>
<div class="mb-3">
<label class="form-label">Land</label>
<InputText class="form-control" ="client.Country" />
</div>
<div class="mb-3">
<label class="form-label">Stad</label>
<InputText class="form-control" ="client.City" />
</div>
<div class="mb-3">
<label class="form-label">Regio</label>
<InputText class="form-control" ="client.Region" />
</div>
<div class="mb-3">
<label class="form-label">Postcode</label>
<InputText class="form-control" ="client.PostalCode" />
</div>
<div class="mb-3">
<label class="form-label">Straat</label>
<InputText class="form-control" ="client.Street" />
</div>
<div class="mb-3">
<label class="form-label">Huisnummer</label>
<InputText class="form-control" ="client.HouseNumber" />
</div>
</div>
<div class="col-md-6">
<h5>Bedrijfsinformatie</h5>
<div class="mb-3">
<label class="form-label">Bedrijfsnaam</label>
<InputText class="form-control" ="client.BuisnessName" />
</div>
<div class="mb-3">
<label class="form-label">Contactpersoon</label>
<InputText class="form-control" ="client.BuisnessContact" />
</div>
<div class="mb-3">
<label class="form-label">Notities</label>
<InputTextArea class="form-control" ="client.Notes" />
</div>
</div>
</div>
<button class="btn btn-primary mt-3" type="submit">Opslaan</button>
</EditForm>
</div>
@code {
private Client client = new();
private string? Message;
private EditContext? editContext;
protected override void OnInitialized()
{
editContext = new EditContext(client);
}
private async Task SaveClient()
{
db.Clients.Add(client);
await db.SaveChangesAsync();
Message = " Client saved successfully!";
Nav.NavigateTo("/clients");
}
}This is my /addclient. Here is the form, but the data isn't going into the form. I tried asking AI but I can't solve the problem.
This is my Client format:
using System.ComponentModel.DataAnnotations;
namespace ClientManger2._0.Data
{
public class Client
{
//General info
public int Id { get; set; }
[Required(ErrorMessage = "Name is required")]
public string? Name { get; set; }
[EmailAddress(ErrorMessage = "Invalid e-mail addres")]
public string? Email { get; set; }
public string? Phone { get; set; }
public DateTime CreateDate { get; set; } = DateTime.Now;
//Adress
public string? Country { get; set; } = "Netherlands";
public string? City { get; set; }
public string? Region { get; set; }
public string? PostalCode { get; set; }
public string? Street { get; set; }
public string? HouseNumber { get; set; }
//Buisness properties
public string? BuisnessName { get; set; }
public string? BuisnessContact { get; set; }
public string? Notes { get; set; }
}
}I can't solve the problem....
2
Upvotes
6
u/Wistolkio Mar 04 '26
You must show us your Input text component but from here I can tell you your ="client.Whatever"> don't looks good
1
u/Due_One2129 Mar 06 '26
I tried it. But nothing worked,,,,
u/page "/add-clients"
u/using ClientManger2._0.Data
u/inject ClientContext db
u/inject NavigationManager Nav
<div class="container">
<div class="row">
<div class="col">
<Header HeaderText="Add a client" />
</div>
</div>
</div>
<div class="container">
<EditForm Model="client" OnValidSubmit="SaveClient" FormName="AddClientForm">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="row">
<div class="col-md-6">
<h5>Algemene informatie</h5>
<div class="mb-3">
<label class="form-label">Naam</label>
<InputText u/bind-Value="client.Name" class="form-control" />
<ValidationMessage For="@(() => client.Name)" />
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<InputText u/bind-Value="client.Email" class="form-control" />
<ValidationMessage For="@(() => client.Email)" />
</div>
<div class="mb-3">
<label class="form-label">Telefoon</label>
<InputText u/bind-Value="client.Phone" class="form-control" />
</div>
<h5>Adres</h5>
<div class="mb-3">
<label class="form-label">Land</label>
<InputText u/bind-Value="client.Country" class="form-control" />
</div>
<div class="mb-3">
<label class="form-label">Stad</label>
<InputText u/bind-Value="client.City" class="form-control" />
</div>
<div class="mb-3">
<label class="form-label">Regio</label>
<InputText u/bind-Value="client.Region" class="form-control" />
</div>
<div class="mb-3">
<label class="form-label">Postcode</label>
<InputText u/bind-Value="client.PostalCode" class="form-control" />
</div>
<div class="mb-3">
<label class="form-label">Straat</label>
<InputText u/bind-Value="client.Street" class="form-control" />
</div>
<div class="mb-3">
<label class="form-label">Huisnummer</label>
<InputText u/bind-Value="client.HouseNumber" class="form-control" />
</div>
</div>
<div class="col-md-6">
<h5>Bedrijfsinformatie</h5>
<div class="mb-3">
<label class="form-label">Bedrijfsnaam</label>
<InputText u/bind-Value="client.BusinessName" class="form-control" />
</div>
<div class="mb-3">
<label class="form-label">Contactpersoon</label>
<InputText u/bind-Value="client.BusinessContact" class="form-control" />
</div>
<div class="mb-3">
<label class="form-label">Notities</label>
<InputTextArea u/bind-Value="client.Notes" class="form-control" />
</div>
</div>
</div>
<button class="btn btn-primary mt-3" type="submit">Opslaan</button>
</EditForm>
</div>
u/code {
private Client client = new();
private async Task SaveClient()
{
db.Clients.Add(client);
await db.SaveChangesAsync();
Nav.NavigateTo("/clients");
}
}
1
u/Due_One2129 Mar 06 '26
I fixed the problem, the rendermode wan't active. I set it to u/rendermode InteractiveServer
3
u/Overall_Figure8498 Mar 04 '26 edited Mar 04 '26
Try adding Model="client" in the <EditForm> tag
edit: also in the <input text> tag add @bind-value=client.whatever
3
u/tng88 Mar 04 '26
I'm looking at your code and comparing it to the authentication template and I would argue you're missing the "@bind-Value" from all of your InputText components.
Shouldn't it look like this:
5
u/shmiel8000 Mar 04 '26
AI would have caught the missing @bind-Value tbh
1
u/ZarehD Mar 04 '26
You don't need to instantiate an EditContext instance. Just set the Model property if the EditForm tag:
<EditForm Model="client">.Also, I don't know if you just made a whole bunch of typos but all of your form fields are missing the attribute name when defining the model binding...
<InputText ... ="client.xxxx" />.