英文:
check box not display checked although it have value true on database?
问题
我在使用 .NET Core 7 开发 Blazor 应用程序时遇到了一个问题,即当尝试检索复选框布尔值 IsActive 的值时出现了问题。
在数据库中,IsActive 的值为 1,但在 Blazor 应用程序中,复选框显示为 false。
如何解决这个问题?
我的代码如下:
我的问题是,尽管数据库中存在值为 1(已选中),但复选框显示为未选中。
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<EditForm Model="@sn" OnValidSubmit="submit">
<div class="modal-body">
<div class="d-flex flex-row bd-highlight mb-3">
<div class="p-2 w-100 bd-highlight">
<div class="form-group row">
<div class="input-group mb-3">
<div class="input-group-text">
<input type="checkbox" aria-label="IsActive" id="IsActive" @bind-value="@sn.IsActive" />
</div>
<label class="form-check-label" for="IsActive">IsActive?</label>
</div>
</div>
</div>
</div>
</div>
</EditForm>
</div>
@code
{
private bool IsActive { get; set; }
private ServerNames sn = new ServerNames();
private void editClick()
{
if (IsActive == true)
{
sn.IsActive = true;
}
else
{
sn.IsActive = false;
}
}
}
请帮助解决这个问题。
英文:
I work on blazor app
with .net core 7. i face issue when retrieve the value of check box boolean IsActive .
on database is active is 1 but on blazor app check box display false
so How to solve this issue
my code as below :
my issue is check box display no checked although it exist on database as 1 (checked)
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<EditForm Model="@sn" OnValidSubmit="submit">
<div class="modal-body">
<div class="d-flex flex-row bd-highlight mb-3">
<div class="p-2 w-100 bd-highlight">
<div class="form-group row">
<div class="input-group mb-3">
<div class="input-group-text">
<input type="checkbox" aria-label="IsActive" id="IsActive" @bind-value="@sn.IsActive" />
</div>
<label class="form-check-label" for="IsActive">IsActive?</label>
</div>
</div>
</div>
</div>
</div>
</EditForm>
</div>
</div>
</div>
@code
{
private bool IsActive { get; set; }
private ServerNames sn = new ServerNames();
private void editClick()
{
if (IsActive == true)
{
sn.IsActive = true;
}
else
{
sn.IsActive = false;
}
}
}
答案1
得分: 2
<EditForm ...
是一个内置组件,旨在与相应的组件一起使用,例如 <InputText...
。在您的情况下,<InputCheckbox @bind-Value="@sn.IsActive"
。Value 是 InputCheckbox 的属性。
要绑定到 <input type="checkbox"
,只需使用 @bind=
<input type="checkbox" ... @bind="@sn.IsActive" />
英文:
<EditForm ...
is a built-in component designed to be used with the corresponding components for example <InputText...
. In your case <InputCheckbox @bind-Value="@sn.IsActive"
. Value is a property of InputCheckbox
To bind to a <input type="checkbox"
just use @bind=
<input type="checkbox" ... @bind="@sn.IsActive" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论