gpt4 book ai didi

c# - mvc 中的复选框返回 null

转载 作者:行者123 更新时间:2023-11-30 19:26:11 25 4
gpt4 key购买 nike

我有这样的模型

    public Nullable<bool> Space { get; set; }

public Nullable<bool> Lab { get; set; }

public Nullable<bool> Consultation { get; set; }

public Nullable<bool> Financial { get; set; }

public Nullable<bool> Other { get; set; }

在我看来,我初始化了复选框,您可以在此处看到:

@Html.CheckBoxFor(model => model.Space.Value, new { id = "CheckSpace" })  
@Html.CheckBoxFor(model => model.Lab.Value, new { id = "CheckLab" })
@Html.CheckBoxFor(model => model.Consultation.Value, new { id = "CheckConsultation"})
@Html.CheckBoxFor(model => model.Financial.Value, new { id = "CheckFinancial" })
@Html.CheckBoxFor(model => model.Other.Value, new { id = "CheckOther" })

但是当我将 View 发布到我的 Controller 时,所有复选框的值都是空的,你能帮帮我吗?

enter image description here

最佳答案

编辑

您应该将属性设置为 bool 而不是 bool? 以将它们绑定(bind)到 @Html.CheckBoxFor。助手只理解值 truefalse。然后使用

@Html.CheckBoxFor(model => model.Space, new { id = "CheckSpace" })  
@Html.CheckBoxFor(model => model.Lab, new { id = "CheckLab" })
@Html.CheckBoxFor(model => model.Consultation, new { id = "CheckConsultation"})
@Html.CheckBoxFor(model => model.Financial, new { id = "CheckFinancial" })
@Html.CheckBoxFor(model => model.Other, new { id = "CheckOther" })

助手生成一个

<input type="checkbox" name="Model Name" value="true" /> 

<input type="hidden" name="Model Name" value="false" /> 

当未选中复选框时,会向服务器发送一个值。

您可能也不想为每个复选框设置一个特定的 ID,而是让框架生成一个;您可以使用例如

获取模型属性的 id
@Html.IdFor(model => model.Space)

关于c# - mvc 中的复选框返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24849368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com