Search code examples
dotvvm

Event of PostBack in DotVVM


In my ViewModel I have the following code:

using System;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.Controls.Bootstrap;
using APP_MIS_FACTURAS.DTO.Contoles;
using System.Collections.Generic;
using APP.Models.View;

namespace APP.ViewModels
{

    public class InicioViewModel : DotvvmViewModelBase
    {

        // Variables para la Vista
        private InicioModel inicioModel = new InicioModel();
        private bool constructor = true;
        private SelectDTO[] inicializaLista;

        // Mensaje 1

        [Bind(Direction.ServerToClient)]
        public string AlertText1 { get; set; }

        [Bind(Direction.ServerToClient)]
        public AlertType AlertType1 { get; set; }
        public bool Dismissed1 { get; set; }

        // Mensaje 2
        [Bind(Direction.ServerToClient)]
        public string AlertText2 { get; set; }

        [Bind(Direction.ServerToClient)]
        public AlertType AlertType2 { get; set; }
        public bool Dismissed2 { get; set; }

        // Mensaje 3

        [Bind(Direction.ServerToClient)]
        public string AlertText3 { get; set; }

        [Bind(Direction.ServerToClient)]
        public AlertType AlertType3 { get; set; }
        public bool Dismissed3 { get; set; }


        // Pagina Inicio
        public string usuario { get; set; }
        public string password { get; set; }

        // Recuperar contrasena
        public string correoElectronico { get; set; }

        // Registro de usuario
        public string nombre { get; set; }
        public string apellidoPaterno { get; set; }
        public string apellidoMaterno { get; set; }
        public bool aceptoTerminos { get; set; }

        public SelectDTO[] genero { get; set; }
        public int selectGenero { get; set; } = 0;

        public InicioViewModel()
        {

            if (constructor)
            {
                AutenticarAplicacion();
                Limpiar();
                inicializaLista = CargaCatalogoGenero();
                constructor = false;
            }
        }

        public void Limpiar()
        {
            //Clean Form Data
        }

        public void Autenticar()
        {

            // Operations to validate user


        }

        public void RegistroUsuario()
        {

            //Operations to create a user

        }

        private void AutenticarAplicacion()
        {

           //Operations to validate the status of the application

        }

        private SelectDTO[] CargaCatalogoGenero()
        {
            //Loading catalogs (Call database)

        }

    }
}

I have a constructor called InicioViewModel (). Within this function I initialize the variables inside the viewmodel, but I have the problem that every time I press any event reloads the function. Then wanted to check if it is possible inside the viewmodel I can catch the postback event of view.


Solution

  • The DotvvmViewModelBase class has multiple methods you can override - Init, Load and PreRender. You can find the details about them in the documentation.

    In your sample, move the code from constructor to the Load method and instead of the constructor private field, use Context.IsPostBack. This will allow you to distinguish between initial page load and the postback.

    Please note that if you use buttons in your page and try to call the method on the viewmodel, it will be executed after the Load method. If you need your code to execute after the viewmodel commands, you have to place it in the PreRender method instead. See the diagram in the documentation. The request pipeline is almost the same as in ASP.NET Web Forms.