Our latest


Announcements, Blogs, Releases and much much more...

  • Read our latest news and announcements, hot off the press.

Announcements






Category Blazor FAQ

Published on 10-Oct-2021

Retrieve a selected value of the select control
Retrieve a selected value of the select control
Retrieve a selected value of the select control

How to retrieve a selected value from the select control, you can use either the @bind or @onchange event.

//using @bind


@page "/dropdown"

<select class="form-control" @bind="@selectedString" style="width:150px">

@foreach (var template in templates)

{

<option value=@template>@template</option>

}

</select>


<h5>Selected Country is: @selectedString</h5>


@code {

List<string> templates = new List<string>() { "America", "China", "India", "Russia", "England" };

string selectedString = "America";

}


//using @onchange event


@page "/dropdown"


<select class="form-control" @onchange="@OnSelect" style="width:150px">

@foreach (var template in templates)

{

<option value=@template>@template</option>

}

</select>


<h5>Selected Country is: @selectedString</h5>


@code {

List<string> templates = new List<string>() { "America", "China", "India", "Russia", "England" };

string selectedString = "America";


void OnSelect (ChangeEventArgs e)

{

selectedString = e.Value.ToString();

Console.WriteLine("The selected country is : " + selectedString);

}

}

Created on 10-Oct-2021 Last updated 21-Oct-2021

Assigned Tag
  • Blazor
  • FAQ