javascript - .Net Core 单选按钮总是返回 false

标签 javascript jquery asp.net .net asp.net-mvc

我正在向 View 发送一个列表。 尝试通过表单提交列表中的每个项目。

型号:

 public partial class Model
    {
        public int Id { get; set; }
        public string UId { get; set; }
        public int WId { get; set; }
        public bool IsEnabled { get; set; }

        public virtual AspNetUsers User { get; set; }
        public virtual WModel W { get; set; }
    }

Controller :

public IActionResult UWC()
{
    List<Model> uW = db.UW.Include(x => x.U).Include(x=>x.W).ToList();
    return View(uW);
}

[HttpPost]
public IActionResult UWC(Model uW)
{
    var s = ModelState.ErrorCount;
    return RedirectToAction("UWC");
}

查看

@model List<Model>
            <table  class="table">
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model[0].W.Name)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model[0].W.Type)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model[0].W.Description)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model[0].W.IsActive)
                        </th>
                    </tr>
                </thead>
                <tbody>
                    @for (var item = 0; item < Model.Count(); item++)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(model => model[item].W.Name)
                            </td>
                            <td>
                                @Html.DisplayFor(model => model[item].W.Type)
                            </td>
                            <td>
                                @Html.DisplayFor(model => model[item].W.Description)
                            </td>
                            <td>
                            <form asp-action="UserWidgetConfiguration" asp-controller="UserManagement" method="post">
                                @Html.RadioButtonFor(model => model[item].IsEnabled, true, new { @Name = "IsEnabled",, id = "enabled_" + item } })<label>True</label>
                                @Html.RadioButtonFor(model => model[item].IsEnabled, false, new { @Name = "IsEnabled" ,, id = "disabled_" + item }})<label>False</label>
                                <input type="hidden" asp-for="@Model[item].Id" value="@Model[item].Id" name="Id" />
                                <input type="hidden" asp-for="@Model[item].WidgetId" value="@Model[item].WidgetId" name="WidgetId" />
                                <input type="hidden" asp-for="@Model[item].UserId" value="@Model[item].UserId" name="UserId" />
                            </form>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>
@section Scripts{
    <script>
        $(function () {
            $('input[type=radio]').change(function () {
                $(this).closest('form').submit();
            });
        });
    </script>
}

以上是完整的代码。

单选按钮始终绑定(bind)为 false。

但是, 我正在使用脚本提交单选按钮值更改时的表单:

  <script>
        $(function () {
            $('input[type=radio]').change(function () {
                var data = JSON.stringify($(this).closest('form').serializeArray());
                console.log(data);
                $(this).closest('form').submit();
            });
        });
    </script>

控制台为单选按钮提供正确的值。

[{"name":"[0].IsEnabled","value":"True"},{"name":"Id","value":"1"},{"name":"WId","value":"1"},{"name":"UId","value":"a"},{"name":"__RequestVerificationToken","value":"CfDJ8FLfqW1-k2hNlQsGtRBQVqOKRWmlkQYWTMrEQNcJHWGd7_LPvHfjw3V5gxYAnLjwz7HvjExZDXgmbSMQ1DnEONg7EJfu5OqYm7xntqXIe4IMkD1HD4SHRtihdyzDXHrKu7jNXUwuN1B6H-565O0J0oZsGVCopZqy180oPco29vqyvpcZkZnWEOmVQX6_V3T6AQ"}]

当我期望 Controller 中 IsEnabled 的值为 true 时,我得到的是 false 值。

但是当表单被序列化并显示在控制台中时,该值正如预期的那样为 true。

这是因为事件处理程序吗?

最佳答案

radio 输入名称中的数组前缀 ([0].IsEnabled) 导致输入无法映射到模型中,因为名称预计为 IsEnabled。 作为简单的解决方法,您可以在不使用 HTML 帮助程序的情况下手动创建输入,例如:

<input type="radio" name="IsEnabled" value="True"/> <label>True</label>
<input type="radio" name="IsEnabled" value="False"/> <label>False</label>

请参阅source帮助者了解更多详细信息。

关于javascript - .Net Core 单选按钮总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57873242/

相关文章:

Asp.net core 使用Facebook外部登录时redirect_url错误

javascript - 尽管有固定值,SVG 中的文本元素仍能完美定位自身

javascript - 悬停时淡出/淡入 Div

javascript - $.getJSON 请求不运行,但下一行代码运行

c# - 在 C# 中使用字典时出错

c# - 我将如何通过依赖注入(inject)传递 Web API 请求?

javascript - highcharts , 隐藏图表

JavaScript 全局变量数组

javascript - 是否必须传递 Prop 来 react 功能组件?

javascript - jQuery e.stopPropagation 与 body onclick 冲突