单击 .NET Core 中的单选按钮时未提交表单

我正在尝试在切换(单选按钮)上提交表单。


我使用 JQuery 提交表单如下:


形式:


        @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>

}

控制器:


[HttpPost]

public IActionResult someaction(somemodel s)

{

--

--

}

我在应该触发的控制器动作上放置了断点。


控件不去动作


但是,切换时console.log会打印消息


我不太明白我做错了什么!!!


项目链接

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


.NET 小提琴

dotnetfiddle.net/8IzLE8


阿晨1998
浏览 284回答 2
2回答

斯蒂芬大帝

您的代码中有几个错误:首先,你的代码<form id="myform" action="xx" controller="xxx" method="post">将被渲染成<form id="myform" action="xx" method="post">请注意action属性xx不是xxx/xx。我想您应该使用asp-action和asp-controller来修复您的代码,例如:<form id="myform" asp-action="MyTestActionName" asp-controller="MyTestControlerName" method="post">其次,有两种具有相同idof 的形式,myform最好为 id 分配不同的名称或id使用删除属性$(this).closest("form").submit():@section Scripts {&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; $('input[type=radio]').on('click', function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("trigger works");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).closest("form").submit();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>}最后,当您input使用asp-for以下代码编写TagHelper 时:<input type="hidden" asp-for="@Model[item].a" /><input type="hidden" asp-for="@Model[item].b" />...它会被渲染成类似的东西:<input type="hidden" id="z1__a" name="[1].a" value="a1"><input type="hidden" id="z1__a" name="[1].b" value="b1">...请注意,名称以 为前缀[1].。这会使绑定变得困难。因为您想将单个项目提交到服务器,就个人而言,我建议您应该将其替换@Model[item].为@item.:@for( var i = 0; i < Model.Count(); i++ ){&nbsp; &nbsp; var item = Model[i];&nbsp; &nbsp; <div style="border: 1px solid ;">&nbsp; &nbsp; <form id="myform" asp-action="Test" asp-controller="Home" method="post">&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" asp-for=" @item.a " />&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" asp-for=" @item.b " />&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" asp-for=" @item.c " />&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" asp-for=" @item.d " />&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript