locked
Blazor - Two way binding for <select> RRS feed

  • Question

  • User-1404255201 posted

    I am using Blazor with .NET core 3.0.0-preview6. When I bind a predefined value with <select>, the UI doesn't change as expected. I am not sure where I can change my code. In my Index.razor file:

                    <form>
                       <select @bind="val">
                            <option value="On">On</option>
                            <option value="Off">Off</option>
                        </select>
                        <p>@val</p>
                    </form>
    
    
    @code 
    {
        private string val = "Off";
    }

    When I load the page, the item in the dropdown button is 'On', while the text under dropdown button is 'Off'.

    I am expecting when I load the page, 'Off' option shows up in the dropdown button.

    Please help me with the issue. Thanks in advance.

    Thursday, July 25, 2019 8:26 AM

All replies

  • User1289604957 posted

    Hi,

    https://blazor-tutorial.net/two-way-data-binding

    Best regards,

    Maher

    Thursday, July 25, 2019 2:12 PM
  • User-1404255201 posted

    Removed

    Friday, July 26, 2019 7:57 AM
  • User-1404255201 posted

    Hi,

    Thanks for your answer, but it didn't solve my problem. I am using @bind and it works when I choose in the dropdown button, but it doesn't work when I give a predefined value for the variable. Could you please try the code I attached. It is quite easy and you will know what I mean.

    <form>
        <select @bind="val">      <!- Here val is Off, but dropdown button shows On -->
            <option value="On">On</option>
            <option value="Off">Off</option>
        </select>
        <p>@val</p>         <!- Here val is Off -->
    </form>
    
    
    @code 
    {
        private string val = "Off";
    }
    Friday, July 26, 2019 7:59 AM