与 Business Pack Alert 组件作斗争

Struggling with Business Pack Alert Component

我正在尝试为我们的应用程序创建一些动态逻辑,但是 <bp:Alert> 组件不想合作。

这是主页 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;
    }

}

这是 MasterView 中的 dothtml:

    <%--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>

在 "Child" ViewModel 中我有这个事件:

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

        AlertComponent = await userRoleGroupDetailFacade.CreateUserRoleGroupAsync(UserRoleGroupDetailDTO);

    }

我的 Class 这个组件的代表是:

 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
    }
}

最后是我的外观函数:

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
        };
    }

如您所见,我在 SaveUserRoleGroup 函数中设置了 AlertComponent,但在浏览器中没有任何反应。 我缺少更改 AlertType,但没关系,它仍然具有我在 MasterViewModel 中设置的默认 "AlertType" 信息。 那么为什么警报不显示且不起作用?

您将警报类型绑定到您仅在 viewmodel Init 中设置的 AlertType 属性。因此,当您稍后更改 AlertComponent 时,您没有设置 AlertType。

或者你可以使用一些小技巧

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

我们应该将 DotVVM.BusinessPack 拆分为两个 nuget 包。一种具有核心类型,一种具有 dotvvm 依赖性。