隐藏的表单值不绑定表单 POST

Hidden form values not binding on Form POST

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

型号:

 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; }
    }

控制器:

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>
                                @if (Model[item].IsEnabled)
                                {
                                    @try
                                    {
                                        <form asp-action="UWC" asp-controller="UM" method="post">
                                            <div class="switch switch--horizontal switch--no-label ">
                                                <input asp-for="@Model[item].IsEnabled" type="radio" value="@Model[item].IsEnabled" class="form-control" />
                                                <input asp-for="@Model[item].IsEnabled" type="radio" value="false" class="form-control" />
                                                <span class="toggle-outside"><span class="toggle-inside"></span></span>
                                            </div>

                                            <input type="hidden" asp-for="@Model[item].Id" value="@Model[item].Id" />
                                            <input type="hidden" asp-for="@Model[item].WId" value="@Model[item].WId" />
                                            <input type="hidden" asp-for="@Model[item].UId" value="@Model[item].UId" />

                                        </form>
                                    }
                                    catch (Exception e)
                                    {
                                        var s = e.Message;
                                    }
                                }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>
@section Scripts{
    <script>
        $(function () {
            $('input[type=radio]').change(function () {
                $(this).closest('form').submit();
            });
        });
    </script>
}

以上为完整代码

当我使用单选按钮切换提交表单时,只有 IsEnabled 属性 绑定到 POST。

但页面源代码显示所有属性都已正确绑定到视图中,即 Id、Uid、Wid 所有这些属性都已被其值替换。

@Model[item].id不一定按顺序,但我相信item——循环变量决定模型绑定。

当页面源具有模型值时,为什么它没有反映在 POST 上?
如果模型绑定中断,为什么 IsEnabled 属性 被正确绑定?

When I submit the form using radio button toggle, only the IsEnabled property is getting bound on POST.

IsEnable 属性 的 false 值不是通过模型绑定传递的,而是 bool 的默认值。

When the Page source has the model values, why is it not being reflected on POST?

我重现了您的问题,您需要为输入指定 name 属性,以对应于参数 Model 的操作属性。否则模型绑定在您的情况下将不起作用。

@try
{
    <form asp-action="UWTest" asp-controller="Test" method="post">
        <div class="switch switch--horizontal switch--no-label ">
            <input asp-for="@Model[item].IsEnabled" type="radio" name="IsEnabled" value="@Model[item].IsEnabled" class="form-control" />
            <input asp-for="@Model[item].IsEnabled" type="radio" name="IsEnabled" value="false" class="form-control" />
            <span class="toggle-outside"><span class="toggle-inside"></span></span>
        </div>

        <input type="hidden" asp-for="@Model[item].Id" value="@Model[item].Id" name="Id"/>
        <input type="hidden" asp-for="@Model[item].WId" value="@Model[item].WId" name="WId"/>
        <input type="hidden" asp-for="@Model[item].UId" value="@Model[item].UId" name="UId" />

    </form>
}
catch (Exception e)
{
    var s = e.Message;
}