Data Access has been discontinued. Please refer to this page for more information.

Initializing AddEditModel

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

You have already implemented the AddEditModel. What you need to do now is initialize it in the appropriate way depending on the operation it should execute.

Implementing the required methods

  1. In MainViewModel define a new method called AddCar. In this method you need to initialize the AddEditViewModel with null as the car Id. This way the view model will be initialized in add mode and allow you to add a new car to the database. If the add operation is successful the data displayed will be refreshed.

    private void AddCar()
    {
        bool? operationIsSuccessful;
        using (AddEditViewModel addModel = new AddEditViewModel(null))
        {
            operationIsSuccessful = addModel.ShowDialog();
        }
        if (operationIsSuccessful == true)
        {
            this.RetrieveCarsToDisplay();
        }
    }
    
    Private Sub AddCar()
        Dim operationIsSuccessful As Boolean?
        Using addViewModel As New AddEditViewModel(Nothing)
            operationIsSuccessful = addViewModel.ShowDialog()
        End Using
        If operationIsSuccessful Then
            Me.RetrieveCarsToDisplay()
        End If
    End Sub
    
  2. In MainViewModel define a new method called EditCar. This method will initialize AddEditContext in edit mode and if the edit operation is successful the displayed data will be refreshed.

    private void EditCar()
    {
        Car carToEdit = this.SelectedCar;
        bool? operationIsSuccessful;
        using (AddEditViewModel editModel = new AddEditViewModel(carToEdit.CarID))
        {
            operationIsSuccessful = editModel.ShowDialog();
        }
        if (operationIsSuccessful == true)
        {
            this.RetrieveCarsToDisplay();
        }
    }
    
    Private Sub EditCar()
        Dim carToEditId As Integer = Me.SelectedCar.CarID
        Dim operationIsSuccessful As Boolean?
        Using editViewModel As New AddEditViewModel(carToEditId)
            operationIsSuccessful = editViewModel.ShowDialog()
        End Using
        If operationIsSuccessful Then
            Me.RetrieveCarsToDisplay()
        End If
    End Sub
    

Exposing commands to the the view

  1. In MainViewModel add a new property AddCarCommand. Int its getter initialize a RelayCommand object based on the AddCar method.

    public RelayCommand AddCarCommand
    {
        get
        {
            this.command = new RelayCommand(this.AddCar);
            return this.command;
        }
        set
        {
            this.command = value;
        }
    }
    
    Public Property AddCarCommand As RelayCommand
        Get
            Me._command = New RelayCommand(AddressOf Me.AddCar)
            Return Me._command
        End Get
        Set(value As RelayCommand)
            Me._command = value
        End Set
    End Property
    
  2. In MainViewModel add a new property EditCarCommand. In its getter initialize a RelayCommand object based on EditCar and the previously defined IsCarSelected method. The IsCarSelected method determines whether or not the edit command can be executed.

    public RelayCommand EditCarCommand
    {
        get
        {
            this.command = new RelayCommand(this.EditCar, this.IsCarSelected);
            return this.command;
        }
        set
        {
            this.command = value;
        }
    }
    
    Public Property EditCarCommand As RelayCommand
        Get
            Me._command = New RelayCommand(AddressOf Me.EditCar, AddressOf Me.IsCarSelected)
            Return Me._command
        End Get
        Set(value As RelayCommand)
            Me._command = value
        End Set
    End Property
    

Checkpoint

Your sample application can now add new cars to the database or edit existing ones.