title: How can I make Two-way binding of a checkbox in client side Blazor work with a view model
body: <p>My checkbox in Blazor client side is only working one way, from UI to the view model, but not the other.
  I also have a Windows application using the exact same view model, where the two-way binding works perfectly.</p>
  
  <p>The view model is placed in a separate project, which is referenced from both the Blazor client side appliction and the Windows application.</p>
  
  <p>I have implemented the INotifyPropertyChanged interface in my view model.</p>
  
  <p>The observed behaviour is:</p>
  
  <ol>
  <li>When I start my Blazor application, the UI does not reflect the value from the view model and database. If I start my Windows application, it does.</li>
  <li>If I change the value in the database, my Windows application UI will get updated within 30 seconds, but my Blazor will not.</li>
  <li>If I click on the check box in the UI, my view model and database will get updated, both in Blazor and the Windows application.</li>
  </ol>
  
  <p>So, what am I doing wrong in the Blazor client side application?</p>
  
  <p>My Blazor view is quite simple:</p>
  
  <pre><code>@using ViewModels
  @inject IViewModel ViewModel
  
  &lt;div class="bordered"&gt;
      &lt;input type="checkbox" name="chkbox1" @bind="@ViewModel.BooleanValue" /&gt;
      &lt;label for="chkbox1"&gt;Boolean Value&lt;/label&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>My view mode:</p>
  
  <pre><code>namespace ViewModels
  {
      public interface IViewModel : IViewModelBase
      {
          bool BooleanValue{ set; get; }
      }
  
      public class ViewModel : ViewModelBase, IViewModel
      {
          private bool booleanValue;
          private readonly IDomain domainLayer;
  
          public ViewModel(IDomain domainLayer)
          {
              this.domainLayer = domainLayer;
              PropertyChanged += HandleDomainLayerUpdateOnPropertyBooleanValueChanged;
              DomainModelChanged += HandleViewModelUpdateOnDomainModelChanged;
              OnDomainModelChanged();
              UpdateFromDomain();
          }
  
          // Function to make the system check for database changes every 30 seconds
          private void UpdateFromDomain()
          {
              Task.Run(async () =&gt;
              {
                  while (true)
                  {
                      OnDomainModelChanged();
                      await Task.Delay(30000);
                  }
              });
          }
  
          public bool BooleanValue
          {
              set
              {
                  booleanValue= value;
                  OnPropertyChanged("BooleanValue");
              }
              get { return booleanValue; }
          }
  
          private async void HandleDomainLayerUpdateOnPropertyBooleanValueChanged(object sender, EventArgs e)
          {
              await domainLayer.SetBooleanValueAsync(executed);
          }
  
          private async void HandleViewModelUpdateOnDomainModelChanged(object sender, EventArgs e)
          {
              BooleanValue = await domainLayer.GetBooleanValueAsync();
          }
      }
  }
  </code></pre>