I'm creating a blazor server side app and have problems to bind a value between two custom components.
I've looked through different example of how the bind or @bind is supposed to work but I cannot figure out what up to date information on this matter is.
Given a model class User:
public class User
{
[Mapping(ColumnName = "short_id")]
public string ShortId { get; set; }
public User()
{
}
}
I want to build a form which displays all properties of this user and has them in an input so it can be edited and in the end saved to a database.
My Form (parent component) looks like this:
<div class="edit-user-form">
<AnimatedUserInput TbText="@User.ShortId" Placeholder="MHTEE Id"/>
<button class="btn btn-secondary" @onclick="Hide" style="{display:inline-block;}">Back</button>
<button class="btn btn-primary" @onclick="SaveUser" style="{display:inline-block;}">Save</button>
</div>
@code {
[Parameter] public vUser User { get; set; }
[Parameter] public Action Hide { get; set; }
bool _error = false;
void SaveUser()
{
ValidateUserData();
}
void ValidateUserData()
{
_error = string.IsNullOrWhiteSpace(User.ShortId);
}
}
Where AnimatedUserInput is a custom component that looks like this:
<div class="edit-area @FocusClass @WiggleClass">
<input type="text" @bind="@TbText" />
<span data-placeholder="@Placeholder"></span>
</div>
@code {
[Parameter]
public string TbText { get; set; }
[Parameter]
public string Placeholder { get; set; }
}
Now in the Input textbox I correctly see the ShortId of the User object in my parent component.
However if I change the text in the input and click on the Save button which triggers the ValidateUserData method and allows me to look at the current User object I see that no changes have been done in the actual User.ShortId property but only on the input.
Is there any way to bind it so that changes in the input will automatically be applied to the binded property?
I have several of these properies which need to be shown in the form which is why I dont want to hook a custom OnChanged Event for each of those properties.
