Search code examples
dotvvm

Struggling with Business Pack Alert Component


I am trying to make some dynamic logic for our application, but <bp:Alert> component don't want to cooperate.

This is masterpage ViewModel:

public class AdminMasterPageViewModel : DotvvmViewModelBase
{
    public virtual AlertComponent AlertComponent { get; set; }
    public DotVVM.BusinessPack.Controls.AlertType AlertType { get; set; }

    public AdminMasterPageViewModel()
    {
    }

    public override Task Init()
    {
        InitAlertComponents();
        return base.Init();
    }

    private void InitAlertComponents()
    {
        AlertComponent = new AlertComponent
        {
            IsDisplayed = false,
            Text = "Default",
            Type = AlertComponent.Types.Info
        };

        AlertType = DotVVM.BusinessPack.Controls.AlertType.Info;
    }

}

This is dothtml in MasterView:

    <%--MAIN CONTENT BY PLACEHOLDER--%>
    <div class="content-wrapper">

        <bp:Alert Type="{value: AlertType}"
                  IsDisplayed="{value: AlertComponent.IsDisplayed}"
                  AllowDismiss="true"
                  Text="{value: AlertComponent.Text}"
                  AutoHideTimeout="3" />

        <dot:ContentPlaceHolder ID="MainContent" />
    </div>

In "Child" ViewModel I have this event:

 public async void SaveUserRoleGroup()
    {
        UserRoleGroupDetailDTO.AppRoleForUserRoleGroupListDTOs = AppRoleListDTOs.Items.Where(i => i.IsAllowed = true).ToList();

        AlertComponent = await userRoleGroupDetailFacade.CreateUserRoleGroupAsync(UserRoleGroupDetailDTO);

    }

My Class representer for this Component is:

 public class AlertComponent
{
    public bool IsDisplayed { get; set; }
    public string Text { get; set; }
    public Types Type { get; set; }
    public enum Types
    {
        Success, Info, Warning, Danger
    }
}

And Finally my facade function:

public async Task<AlertComponent> CreateUserRoleGroupAsync(UserRoleGroupDetailDTO dto)
    {
        using (var uow = unitOfWorkProvider.Create())
        {
            try
            {
                var userRoleGroup = Mapper.Map<UserRoleGroup>(dto);
                userRoleGroup.PortalSetting = portalSettingRepository.GetById(1);

                repository.Insert(userRoleGroup);

                await uow.CommitAsync();
            }
            catch (Exception e)
            {
                return new AlertComponent
                {
                    Text = "Error",
                    IsDisplayed = true,
                    Type = AlertComponent.Types.Danger
                };
            }
        }
        return new AlertComponent
        {
            Text = "Success",
            IsDisplayed = true,
            Type = AlertComponent.Types.Success
        };
    }

As you can see, I am setting AlertComponent in SaveUserRoleGroup function, but in browser nothing happen. Iam missing changing AlertType, but it doesn't matter,it still has as default "AlertType" Info, which I set in MasterViewModel. So why alert doesn't show and not working?


Solution

  • You bind Alert type to AlertType property which you set in viewmodel Init only. So when you change AlertComponent later, you didn't setup AlertType.

    Or you can use a litle trick

    public AlertType AlertType => (AlertType)AlertComponent.Type;
    

    We should split DotVVM.BusinessPack to two nuget package. One with Core types and One with dotvvm dependency.