复选框未显示选中,尽管在数据库中数值为 true?

huangapple go评论61阅读模式
英文:

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)

    &lt;div class=&quot;modal fade&quot; id=&quot;exampleModal&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;

                &lt;EditForm Model=&quot;@sn&quot; OnValidSubmit=&quot;submit&quot;&gt;
                 
                &lt;div class=&quot;modal-body&quot;&gt;
                    &lt;div class=&quot;d-flex flex-row bd-highlight mb-3&quot;&gt;
                        &lt;div class=&quot;p-2 w-100 bd-highlight&quot;&gt;
                         
                       
                                &lt;div class=&quot;form-group row&quot;&gt;
                                    &lt;div class=&quot;input-group mb-3&quot;&gt;
                                        &lt;div class=&quot;input-group-text&quot;&gt;
                                            &lt;input type=&quot;checkbox&quot; aria-label=&quot;IsActive&quot; id=&quot;IsActive&quot; @bind-value=&quot;@sn.IsActive&quot; /&gt;
                                        &lt;/div&gt;
                                        &lt;label class=&quot;form-check-label&quot; for=&quot;IsActive&quot;&gt;IsActive?&lt;/label&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                       
                      
                        &lt;/div&gt;
                    &lt;/div&gt;
    
                &lt;/div&gt;
                &lt;/EditForm&gt;
    
            &lt;/div&gt;
    
        &lt;/div&gt;
    
    &lt;/div&gt;
    
    @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

&lt;EditForm ... 是一个内置组件,旨在与相应的组件一起使用,例如 &lt;InputText...。在您的情况下,&lt;InputCheckbox @bind-Value=&quot;@sn.IsActive&quot;。Value 是 InputCheckbox 的属性。

要绑定到 &lt;input type=&quot;checkbox&quot;,只需使用 @bind=

&lt;input type=&quot;checkbox&quot; ... @bind=&quot;@sn.IsActive&quot; /&gt;
英文:

&lt;EditForm ... is a built-in component designed to be used with the corresponding components for example &lt;InputText.... In your case &lt;InputCheckbox @bind-Value=&quot;@sn.IsActive&quot;. Value is a property of InputCheckbox

To bind to a &lt;input type=&quot;checkbox&quot; just use @bind=

&lt;input type=&quot;checkbox&quot; ... @bind=&quot;@sn.IsActive&quot; /&gt;

huangapple
  • 本文由 发表于 2023年2月16日 09:02:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定