title: Updating another ViewModel when button clicked
body: <p>I'm trying to update another ViewModel when a button is clicked. That Viewmodel already has a button that updates its own ViewModel but I want the exact same functionallity on another viewModel</p>
  
  <p>Here is my code:</p>
  
  <p>OlyckorViewModel (The view that I want to update and that already has a button that updates it) <strong><code>SearchActiveInvestigationsCommand</code> is the button binding that updates it</strong></p>
  
  <pre><code>public class OlyckorViewModel : NotificationObject
      {
          private readonly ISosServiceDelegate _sosService;
          private readonly IDialogService _dialogService;
          private string _searchConditionDiarienummer;
          private string _searchConditionFartygsnamn;
          private string _searchConditionRegisterbeteckning;
          private int? _searchConditionIMONummer;
          private DateTime? _searchConditionHaendelseDatum;
          private string _searchConditionOlyckshaendelsetypKod;
          private bool _isSearching;
          private bool _isSearchExpanded = true;
          private ObservableCollection&lt;SosListOlycka&gt; _olyckor;
          private SosListOlycka _selectedOlycka;
          private string _statusText;
  
          public OlyckorViewModel(ISosServiceDelegate sosService, ILoggerFacade logger, IDialogService dialogServce)
          {
              logger.Log("Initializing OlyckorViewModel", Category.Debug, Priority.Low);
  
              _sosService = sosService;
              _dialogService = dialogServce;
  
              SearchCommand = new DelegateCommand(ExecuteSearch, CanExecuteSearch);
              SearchActiveInvestigationsCommand = new DelegateCommand(ExecuteSearchActiveInvestigations, CanExecuteSearchActiveInvestigations);
              OpenCommand = new DelegateCommand(ExecuteOpen, CanExecuteOpen);
              DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteDelete);
              CreatePdfReportCommand = new DelegateCommand(ExecuteCreatePdfReport, CanExecuteCreatePdfReport);
          }
  
  
   private void ExecuteSearch()
          {
              if (IsSearching)
                  return;
  
              IsSearching = true;
              StatusText = "Söker olyckor...";
  
              var criteria = new SosOlyckaSearchCriteria();
              criteria.Diarienummer = SearchConditionDiarienummer;
              criteria.Fartygsnamn = SearchConditionFartygsnamn;
              criteria.Registerbeteckning = SearchConditionRegisterbeteckning;
              criteria.IMONummer = SearchConditionIMONummer;
              criteria.HaendelseDatum = SearchConditionHaendelseDatum;
              criteria.OlyckshaendelseTypKod = SearchConditionOlyckshaendelsetypKod;
  
              _sosService.SearchOlyckor(
                  criteria,
                  olyckor =&gt;
                  {
                      HandleResultFromSearch(olyckor);
                  },
                  exception =&gt;
                  {
                      IsSearching = false;
                      StatusText = "Misslyckades att söka olyckor";
                      DialogHelper.ShowException(exception);
                  }
              );
          }
  
  private void HandleResultFromSearch(SosSearchResult&lt;SosListOlycka&gt; olyckor)
          {
              IsSearching = false;
              IsSearchExpanded = false;
              Olyckor = new ObservableCollection&lt;SosListOlycka&gt;(olyckor.Items);
  
              StatusText = Olyckor.Count + " " + (Olyckor.Count == 1 ? "olycka." : "olyckor.");
  
              if (olyckor.IsResultTruncated)
              {
                  var statusTextResultTruncated = "Resultatet var för stort (" + olyckor.OriginalNumberOfHits + " olyckor) och trunkerades på servern.";
                  StatusText += " " + statusTextResultTruncated;
              }
  
              // if search result only contains a single item, it should be opened
              if (Olyckor.Count == 1)
                  DialogHelper.OpenOlyckaDialog(Olyckor[0].OlyckaId);
          }
  </code></pre>
  
  <p>OlyckaViewModel (The viewModel that is supposed to have a save button to update the viewmodel above) <strong><code>SaveCommand</code> is the button binding that I want to update like the button above</strong></p>
  
  <pre><code> public class OlyckaViewModel : DialogWindowViewModel
          {
              private readonly ISosServiceDelegate _sosService;
              private readonly Repository _repository;
              private readonly IDialogService _dialogService;
              private readonly IInteractionService _interactionService;
              private readonly FroCodesViewModel _froCodesViewModel;
              private SosOlycka _model;
              private SosOlycksorsak _selectedOlycksorsak;
              private SosOlycksorsak _selectedHuvudorsak;
              private SosStegIOlycksfoerlopp _selectedStegIOlycksfoerlopp;
              private SosStegIOlycksfoerlopp _selectedInledandeSteg;
              private SosOlycka _selectedOtherOlycka;
              private OlyckorViewModel _olyckorlist;
  
              private readonly ObservableCollection&lt;CheckBoxListItemViewModel&lt;TrsFartygsunderkategori&gt;&gt; _fartygsunderkategorier = new ObservableCollection&lt;CheckBoxListItemViewModel&lt;TrsFartygsunderkategori&gt;&gt;();
              private int? _olycksrapportIdToHaemtmarkeraWhenSaving;
              private ObservableCollection&lt;SosListOlycka&gt; _olyckor;
              private SosListOlycka _selectedOlycka;
              private bool _isSearching;
              private bool _isSearchExpanded = true;
  
  
              private readonly List&lt;KnownValue&gt; _oestEllerVaest = new List&lt;KnownValue&gt;()
          {
              new KnownValue("Välj", null),
              new KnownValue("E", "E"),
              new KnownValue("W", "W")
          };
  
              private readonly List&lt;KnownValue&gt; _nordEllerSyd = new List&lt;KnownValue&gt;()
          {
              new KnownValue("Välj", null),
              new KnownValue("N", "N"),
              new KnownValue("S", "S")
          };
  
              public OlyckaViewModel(ISosServiceDelegate sosService, Repository repository, IDialogService dialogService, IInteractionService interactionService)
              {
                  _sosService = sosService;
                  _repository = repository;
                  _dialogService = dialogService;
                  _interactionService = interactionService;
                  _froCodesViewModel = new FroCodesViewModel(ServiceLocator.Current.GetInstance&lt;ISitsServiceDelegate&gt;());
  
  
  
                  SaveCommand = new DelegateCommand(ExecuteSave);
                  ReloadCommand = new DelegateCommand(ExecuteReload, CanExecuteIfExistingOlycka);
                  CloseCommand = new DelegateCommand(Close);
                  DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteIfExistingOlycka);
                  AvslutaUtredningCommand = new DelegateCommand(ExecuteAvslutaUtredning, CanExecuteIfExistingOlycka);
                  CreatePdfReportCommand = new DelegateCommand(ExecuteCreatePdfReport, CanExecuteIfExistingOlycka);
                  AddOlycksorsakCommand = new DelegateCommand(ExecuteAddOlycksorsak);
                  RemoveOlycksorsakCommand = new DelegateCommand(ExecuteRemoveOlycksorsak, CanExecuteRemoveOlycksorsak);
                  AddStegIOlycksfoerloppCommand = new DelegateCommand(ExecuteAddStegIOlycksfoerlopp);
                  RemoveStegIOlycksfoerloppCommand = new DelegateCommand(ExecuteRemoveStegIOlycksfoerlopp, CanExecuteRemoveStegIOlycksfoerlopp);
                  FetchFartygsinformationFromSitsCommand = new DelegateCommand(ExecuteFetchFartygsinformationFromSits);
                  NewOlyckaFromHaendelseCommand = new DelegateCommand(ExecuteNewOlyckaFromHaendelse);
                  OpenOtherOlyckaCommand = new DelegateCommand(ExecuteOpenOtherOlycka, CanExecuteOpenOtherOlycka);
                  MoveOlyckaToNewHaendelseCommand = new DelegateCommand(ExecuteMoveOlyckaToNewHaendelse);
                  MoveOlyckaToOtherHaendelseCommand = new DelegateCommand(ExecuteMoveOlyckaToOtherHaendelse);
                  MarkSelectedOlycksorsakAsHuvudorsakCommand = new DelegateCommand(ExecuteMarkSelectedOlycksorsakAsHuvudorsak);
                  MarkSelectedStegIOlycksfoerloppAsInledandeStegCommand = new DelegateCommand(MarkSelectedStegIOlycksfoerloppAsInledandeSteg);
                  SearchActiveInvestigationsCommand = new DelegateCommand(ExecuteSearchActiveInvestigations, CanExecuteSearchActiveInvestigations);
              }
  
  
  
              public DelegateCommand SaveCommand { get; private set; }
              public DelegateCommand ReloadCommand { get; private set; }
              public DelegateCommand CloseCommand { get; private set; }
              public DelegateCommand DeleteCommand { get; private set; }
              public DelegateCommand AvslutaUtredningCommand { get; private set; }
              public DelegateCommand CreatePdfReportCommand { get; private set; }
              public DelegateCommand AddOlycksorsakCommand { get; private set; }
              public DelegateCommand RemoveOlycksorsakCommand { get; private set; }
              public DelegateCommand AddStegIOlycksfoerloppCommand { get; private set; }
              public DelegateCommand RemoveStegIOlycksfoerloppCommand { get; private set; }
              public DelegateCommand FetchFartygsinformationFromSitsCommand { get; private set; }
              public DelegateCommand NewOlyckaFromHaendelseCommand { get; private set; }
              public DelegateCommand OpenOtherOlyckaCommand { get; private set; }
              public DelegateCommand MoveOlyckaToNewHaendelseCommand { get; private set; }
              public DelegateCommand MoveOlyckaToOtherHaendelseCommand { get; private set; }
              public DelegateCommand MarkSelectedOlycksorsakAsHuvudorsakCommand { get; private set; }
              public DelegateCommand MarkSelectedStegIOlycksfoerloppAsInledandeStegCommand { get; private set; }
              public DelegateCommand SearchActiveInvestigationsCommand { get; private set; }
              public DelegateCommand SearchCommand { get; private set; }
  
              public FroCodesViewModel FroCodesViewModel { get { return _froCodesViewModel; } }
  
              public OlyckorViewModel OlyckorViewModel { get { return _olyckorlist; } }
              public SosOlycka Model
              {
                  get { return _model; }
                  private set
                  {
                      if (value != _model)
                      {
                          _model = value;
                          RaisePropertyChanged(() =&gt; Model);
                          RaisePropertyChanged(() =&gt; IsExistingOlycka);
                          RaiseCanExecuteChangedForOlyckaCommands();
  
                          LoadDescriptionsForFroCodes();
  
                          SynchronizeFromModel();
                      }
                  }
              }
  
  
  
  private void ExecuteSave()
              {
                  if (IsBusy)
                      throw new InvalidOperationException("Cannot save olycka, is already busy");
  
                  SynchronizeToModel();
  
                  // Validate ad-acta
                  List&lt;string&gt; missingAdActaFields = null;
                  if (Model.MyndighetensUtredningAvslutad.HasValue)
                  {
                      missingAdActaFields = DataHelper.ValidateAdActa(Model);
                  }
  
                  // Validate skrovskada
                  List&lt;string&gt; missingSkrovskadaFields = null;
                  if (Model.SosFartygsskada.Skrovskada == "J")
                  {
                      missingSkrovskadaFields = DataHelper.ValidateSkrovskada(Model);
                  }
  
                  if ((missingAdActaFields != null &amp;&amp; missingAdActaFields.Count &gt; 0)
                      || (missingSkrovskadaFields != null &amp;&amp; missingSkrovskadaFields.Count &gt; 0))
                  {
                      // Bring up window of missing fields (or update if already open)
                      var vm = _dialogService.GetOpenDialogs().OfType&lt;MissingFieldsViewModel&gt;().FirstOrDefault();
                      if (vm != null)
                      {
                          vm.AdActaFields = missingAdActaFields;
                          vm.SkrovskadaFields = missingSkrovskadaFields;
                          _dialogService.Activate(vm);
                      }
                      else
                      {
                          vm = ServiceLocator.Current.GetInstance&lt;MissingFieldsViewModel&gt;();
                          vm.AdActaFields = missingAdActaFields;
                          vm.SkrovskadaFields = missingSkrovskadaFields;
                          _dialogService.Show(vm);
                      }
  
  
  
                      //
  
  
  
  
                      // Cancel save operation
                      return;
                  }
                  else
                  {
                      // Close window with missing fields if open)
                      var vm = _dialogService.GetOpenDialogs().OfType&lt;MissingFieldsViewModel&gt;().FirstOrDefault();
                      if (vm != null)
                      {
                          vm.Close();
                      }
                  }
  
                  SetBusy("Sparar ändringar...");
  
                  _sosService.SaveOlycka(
                      Model,
                      result =&gt;
                      {
                          // load olycka using the id returned from save method (useful if olycka was new / not previously persisted)
                          ResetBusy();
  
                          if (IsGoingToHaemtmarkeraWhenSaving)
                          {
                              SetBusy("Markerar olycksrapport som hämtad...");
  
                              var rosService = ServiceLocator.Current.GetInstance&lt;IRosServiceDelegate&gt;();
                              rosService.HaemtmarkeraOlycksrapport(
                                  OlycksrapportIdToHaemtmarkeraWhenSaving.Value,
                                  result,
                                  () =&gt;
                                  {
                                      ResetBusy();
                                      OlycksrapportIdToHaemtmarkeraWhenSaving = null;
                                      LoadOlycka(result);
                                  },
                                  haemtmarkeraException =&gt;
                                  {
                                      ResetBusy();
                                      DialogHelper.ShowException(haemtmarkeraException);
                                  }
                              );
                          }
                          else
                          {
                              LoadOlycka(result);
                          }
                      },
                      exception =&gt;
                      {
                          ResetBusy();
                          DialogHelper.ShowException(exception);
                      }
                  );
  
                  ExecuteSearchActiveInvestigations();
              }
  </code></pre>
  
  <p>I know it's alot of code, I just wanted to make sure not to miss anything.</p>
  
  <p>I tried to copy the same code from the already working button to the button I want to work the same but it didn't work and I suspect it's because I didn't tell it to update another view and not itself. I could be wrong tho.</p>
  
  <p>I appreciate any help,</p>