I'm using the Asp.Net Core Razor Pages for one of my static website.
I have one drop-down on my page and I bind it using the following code.
[BindProperty]
public string selectedFilter { get; set; }
public List<SelectListItem> Options { get; set; }
public void OnGet()
{
this.Options = new List<SelectListItem> {
new SelectListItem { Text = "Test1", Value = "1" },
new SelectListItem { Text = "Test2", Value = "2" },
new SelectListItem { Text = "Test3", Value = "3" },
};
selectedFilter = "3";
}
In cshtml
<select asp-for="selectedFilter" asp-items="Model.Options"></select>
Once the page load - The Dropdown is rendered properly
Now, I just wanted to know how to handle the onchange
event of Dropdown so it calls the OnGet/OnPost
method of Razor page and I can get the value in selectedFilter
property.
Basically I am doing the content filtering on change event of Dropdown.
I used the https://www.learnrazorpages.com/razor-pages/forms/select-lists as reference.
Thank you in advance.
Update (Answer)
I got success by putting the SELECT into FORM element like below.
<form method="post">
<!-- Other HTML -->
<select asp-for="selectedFilter" asp-items="Model.Options" onchange="this.form.submit();"></select>
<!-- Other HTML -->
</form>
And added the following OnPost method into CS file.
public void OnPost()
{
//Here you will get the selected value into selectedFilter
}
Thank you everyone for helping me out