Search code examples
javascriptjqueryrazorasp.net-coreasp.net-core-mvc

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.


Solution

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