0
votes

How I can add default text in Blazor InputText. I add 123456 in InputText but when blazor page loaded InputText shown null.

Null Text

<div class="form-group">
<label for="pass">Phone Number:</label>
<div>
       <InputText @bind-Value="userInfo.PhoneNumber" style="width:100%;">123456</InputText>
        <ValidationMessage For="@(() => userInfo.PhoneNumber)" />
</div>
</div>
1
As your binding to userInfo.PhoneNumber just set that and remove the content. <InputText @bind-Value="userInfo.PhoneNumber" /> - Brian Parker
Thanks for your reply <InputText @bind-Value="123456" style="width:100%;"></InputText> but it got error. - گلی
No 'userInfo.PhoneNumber = 12345' prior to showing the form or in the class initializer. - Brian Parker
Thanks. I add <InputText @bind-Value="userInfo.PhoneNumber = 123456" but again i get error : Cannot implicitly convert type 'int' to 'string'. best regards for you. - گلی

1 Answers

1
votes

Make whatever you want the default to be in your binded Variable

<input @bind="phonNumber" />



@code{
string phoneNumber = "12345678";

}

or in your case you can keep the bound number to userInfo.PhoneNumber and just set that variable to whatever default you want onInitialized

<input @bind="userInput.phoneNumber" />

@code
{

UserInput userInput {get; set;}
  protected override void OnInitialized()
  {
    userInput.phoneNumber = "123456";
  }
}

Of course you will need to define userInput and make sure it has the field or property "phoneNumber".