javascript - 单击 .NET Core 中的单选按钮时 FORM 未提交
问题描述
我正在尝试在切换(单选按钮)上提交表单。
我正在使用 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 FIDDLE
dotnetfiddle.net/8IzLE8
解决方案
您的代码中有几个错误:
首先,你的代码
<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">
其次,有两种具有相同of 的多种形式,id
myform
最好为 id 分配不同的名称或id
使用删除属性$(this).closest("form").submit()
:
@section Scripts {
<script type="text/javascript">
$('input[type=radio]').on('click', function () {
console.log("trigger works");
$(this).closest("form").submit();
});
</script>
}
最后,当您使用以下方式编写input
TagHelper时asp-for
:
<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++ ) { 变量项 = 模型 [i]; <div style="border: 1px solid ;"> <form id="myform" asp-action="Test" asp-controller="Home" method="post"> <input type="hidden" asp-for=" @item.a " /> <input type="hidden" asp-for=" @item.b " /> <input type="hidden" asp-for=" @item.c " /> <input type="hidden" asp-for=" @item.d " /> ...
推荐阅读
- angular - 从 Node_Modules 替换文件
- reactjs - 无法在设备中调试
- javascript - 如何通过重用相同的功能在画布上动态绘制?
- python - 避免 @dataclass 变量声明的 Python lint 警告
- apache-kafka - 话题消费率
- gcc - glib 版本不是最低要求
- reactjs - 即使函数位于“src”值更改之前,本地图像也不会触发 onload 事件
- python - 如何使用 numba 加速积分,使用数组中的值重新定义积分
- hadoop - 如何修复 Jython 和 HBase 中的“ImportError: No module name apache”错误
- php - 将 public_html/image 中的图片插入 mysql