How do I call a function or a method on a select list if the value changes?

只谈情不闲聊 提交于 2019-12-11 15:12:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!