【发布时间】:2020-04-03 00:34:38
【问题描述】:
我正在尝试显示引导模式,然后绑定其按钮。但我无法通过显示模态的第一步。我正在使用 .net core 3.1 的 Blazor 客户端模板。我有一个名为 Modal.razor 的页面,其中包含我从 getbootstrap.com 找到的引导模式。
@if (Show)
{
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
}
@code {
[Parameter]
public bool Show { get; set; } = false;
}
我在 index.razor 文件中调用了模态
@page "/"
<button @onclick="(()=>switchModal=!switchModal)">Switch Modal</button>
<Modal Show="switchModal"/>
@code{
bool switchModal = false;
}
你可能会说 StateHasChanged 应该在这里被调用。但即使我将模态代码复制并粘贴到 index.razor 中,我也看不到任何内容。
【问题讨论】:
-
您的代码的问题是,您所做的只是切换是否将 HTML 发送到客户端,但是使用引导程序,模态 HTML 始终在页面上,并且要么使用带有 $('#modal').modal() 的 javascript 触发,要么使用应该打开它的按钮上的 data-toggle 和 data-target 标签触发。
-
你可以使用这个 Nuget 包:nuget.org/packages/Majorsoft.Blazor.Components.Modal 它还支持背景和动画... Docs:github.com/majorimi/blazor-components/blob/master/.github/docs/…
标签: razor .net-core blazor blazor-client-side