如何在Blazor客户端应用程序中使用Bootstrap模态? [英] How to use Bootstrap modal in Blazor client app?
问题描述
我试图显示引导程序模式,然后绑定其按钮.但是我无法通过显示模态的第一步.我正在使用.net core 3.1的Blazor客户端模板.我有一个名为Modal.razor的页面,其中包含从getbootstrap.com找到的引导程序模式.
I am trying to show bootstrap modal then bind its buttons. But I cannot pass the first step showing the modal. I am using Blazor client template of .net core 3.1. I have a page named Modal.razor which contains the bootstrap modal I found from 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文件中称模态
An I called the modal in the index.razor file
@page "/"
<button @onclick="(()=>switchModal=!switchModal)">Switch Modal</button>
<Modal Show="switchModal"/>
@code{
bool switchModal = false;
}
您可能会说应在此处调用StateHasChanged.但是,即使我将模式代码复制并粘贴到index.razor中,也看不到任何内容.
You might say StateHasChanged should be called here. But even if I copy and paste the modal code in the index.razor, I won't see anything.
推荐答案
可能有更好的方法,但这是一个使您入门的有效示例:
There is likely a better way to do this, but here's a working example to get you started:
页面:
@page "/modal-test"
<BlazorApp1.Components.Modal @ref="Modal"></BlazorApp1.Components.Modal>
<button @onclick="() => Modal.Open()">Open Modal</button>
@code {
private BlazorApp1.Components.Modal Modal { get; set; }
}
组件:
<div class="modal @ModalClass" tabindex="-1" role="dialog" style="display:@ModalDisplay">
<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" @onclick="() => Close()">Close</button>
</div>
</div>
</div>
</div>
@if (ShowBackdrop)
{
<div class="modal-backdrop fade show"></div>
}
@code {
public Guid Guid = Guid.NewGuid();
public string ModalDisplay = "none;";
public string ModalClass = "";
public bool ShowBackdrop = false;
public void Open()
{
ModalDisplay = "block;";
ModalClass = "Show";
ShowBackdrop = true;
StateHasChanged();
}
public void Close()
{
ModalDisplay = "none";
ModalClass = "";
ShowBackdrop = false;
StateHasChanged();
}
}
解决此问题的另一种方法是使用JSInterop调用$('#modalId').modal()
Another option to go about this, would be to use JSInterop to call $('#modalId').modal()
通过执行以下操作,可以使组件的每个版本都有唯一的ID:
<div id="bootstrap-modal-@Guid"
然后使用保存的ID通过jQuery调用.modal().
You could have each version of the component have a unique id by doing something like this:
<div id="bootstrap-modal-@Guid"
then use the saved ID to call .modal() with jQuery.
这篇关于如何在Blazor客户端应用程序中使用Bootstrap模态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!