Tuesday, 3 September 2019

FORM not submitting on click of radio button in .NET Core

I am attempting to submit a form on toggle(radio button).

I am using JQuery to submit the form as follows:

Form:

        @for (var item = 0; item < Model.Count(); item++)
        {
        <form id="myform" action="xx" controller="xxx" method="post">
            <input type="hidden" asp-for="@Model[item].a" />
            <input type="hidden" asp-for="@Model[item].b" />
            <input type="hidden" asp-for="@Model[item].c" />
            <input type="hidden" asp-for="@Model[item].d"  />
            <tr>
                <td>
                    @Html.DisplayFor(model => model[item].a)
                </td>
                <td>
                    @Html.DisplayFor(model => model[item].b)
                </td>
                <td>
                    @Html.DisplayFor(model => model[item].c)
                </td>
                <td>
                    @if(Model[item].istrue)
                    {
                        <input asp-for="@Model[item].istrue" type="radio" value="@Model[item].istrue" class="form-check form-control"/> @Model[item].istrue
                        <input asp-for="@Model[item].istrue" type="radio" value="False" class="form-check form-control" />
                    }
                    else  
                    {
                        <input asp-for="@Model[item].istrue" type="radio" value="@Model[item].istrue" class="form-check form-control" /> @Model[item].istrue
                        <input asp-for="@Model[item].istrue" type="radio" value="True" class="form-check form-control" />
                    }
                </td>
            </tr>
        </form>

Javascript:

@section Scripts {
        <script type="text/javascript">
    $('input[type=radio]').on('click', function () {
        console.log("trigger works");
        $(this).closest("form").submit();
    });
</script>
}

Controller:

[HttpPost]
public IActionResult someaction(somemodel s)
{
--
--
}

I have placed breakpoints at the controller action which is supposed to be triggered.

The control does not go to the action

However, when toggled the console.log message is printed

I don't quite understand what I am doing wrong!!!

Project Link https://github.com/dev-agk/WebFormList



from FORM not submitting on click of radio button in .NET Core

No comments:

Post a Comment