简体   繁体   中英

Submit form on Radio Button click in ASP.Net Core

Using ASP.Net Core I have a page with a few radio buttons that would alter the display of the page and what I would like to do is if a user were to click the radio button the page would be refreshed to update the page with some new data based on whichever radio button (option) were clicked.

To achieve this would it be more suitable with a form submit? Or more suitable to handle this interaction using JavaScript?

For a form submit I had tried something like this, but it doesnt submit the form:

 <form method="post" asp-action="MethodName" asp-controller="ControllerName"> @foreach (var item in Model.FormOptions) { <input asp-for="@Model.SelectedFormOption" type="radio" value="@item.ID" /> @item.Name } </form>

I could achieve the submit with a button however I'd prefer to submit the form on the click of the radio button.

Here you can take example:

 $('input[type=radio]').on('change', function() {
   $("form").submit();
});
$(document).ready(function () {
    $('input[type=radio]').click(function () {
        document.getElementById("formIDHere").submit();
    });
});

One dirty trick to do it within HTML:

use onclick="this.form.submit()" directly on inputs...

<input asp-for="@Model.SelectedFormOption" type="radio" value="@item.ID" onclick="this.form.submit()" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM