问题
I have a scenario where if I change the value of a select list I want to be able to clear the model or any values I want to clear. Is it possible to call a function or a method in a select tag helper? Something like this:
<select asp-for="Input.DriverName" class="form-control"
asp-items="Model.DriverNotUser"
onchange="myfunction()">
</select>
回答1:
To call a javascript function :
<select class="form-control"
asp-for="DriverName"
asp-items="Model.DriverNotUser"
onchange="myfunction();">
</select>
@section Scripts{
function myfunction() {
// Do whatever...
}
}
To submit to the backend, put the select list in a form and call submit:
<!-- submit to the OnGet() method -->
<!-- add method="post" to submit to the OnPost() method -->
<form>
<select class="form-control"
asp-for="DriverName"
asp-items="Model.DriverNotUser"
onchange="this.form.submit();">
</select>
</form>
To call a custom method in the backend:
<!-- submit to the OnPostMyMethod() method -->
<form method="post" asp-page-handler="MyMethod">
<select class="form-control"
asp-for="DriverName"
asp-items="Model.DriverNotUser"
onchange="this.form.submit();">
</select>
</form>
Backend method:
public IActionResult OnPostMyMethod()
{
// ...
}
来源:https://stackoverflow.com/questions/58618679/how-do-i-call-a-function-or-a-method-on-a-select-list-if-the-value-changes