DotVVM中的PostBack事件

Event of PostBack in DotVVM

在我的 ViewModel 中,我有以下代码:

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)

        }

    }
}

我有一个名为 InicioViewModel () 的构造函数。在这个函数中,我初始化了视图模型中的变量,但我遇到了一个问题,即每次我按下任何事件时都会重新加载该函数。然后想检查是否有可能在视图模型中我可以捕获视图的回发事件。

DotvvmViewModelBase class 有多种方法可以覆盖 - InitLoadPreRender。您可以在 documentation 中找到有关它们的详细信息。

在您的示例中,将代码从构造函数移至 Load 方法,并使用 Context.IsPostBack 而不是 constructor 私有字段。这将使您能够区分初始页面加载和回发。

请注意,如果您在页面中使用按钮并尝试调用视图模型上的方法,它将在 Load 方法之后执行。如果您需要在 viewmodel 命令之后执行代码,则必须将其放在 PreRender 方法中。请参阅 documentation 中的图表。请求管道与 ASP.NET Web 表单中的几乎相同。