Asp.net MVC 将复选框数据放入布尔值列表中

Bre*_*cke 3 asp.net-core-mvc

我正在尝试将未知数量的复选框绑定到布尔值列表。经过数小时的搜索,我还没有找到任何解决方案。

在我看来,这是代码的相关部分:

        @foreach (Device d in Data.GetDevices())
    {
        <label asp-for="NewRegistration.Devices">@d.Name</label>
        <input asp-for="NewRegistration.Devices" class="checkbox" type="checkbox" />
    }
Run Code Online (Sandbox Code Playgroud)

循环有效,但是当我更改类型时,如果复选框的输入给了我这个例外:

InvalidOperationException: 意外的 'asp-for' 表达式结果类型 'System.Collections.Generic.List`1[[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' for . 如果“type”是“checkbox”,“asp-for”必须是“System.Boolean”类型。

我尝试放入我的数据的列表包含以下代码:

        private List<bool> _devices;

    public List<bool> Devices
    {
        get { return _devices; }
        set {_devices = value; }
    }
Run Code Online (Sandbox Code Playgroud)

所以我的问题是如何将这些复选框中的值添加到我的布尔值列表中。当我不指定输入的类型时,我不会收到任何错误,但输入的类型是文本。如果我在这些输入框中输入 true 或 false,我的代码就可以正常工作。但我想要一个复选框,而不是要求用户输入 true 或 false。

感谢您的时间!

编辑:我的问题很难解释。我正在验证表单并将其绑定到演示模型 PMRegistration 中定义的注册类的实例。如果片段不够清楚,我会将完整代码放在下面

报名类:

  public class Registration
{

    public int Id { get; set; }
    [Required(ErrorMessage = "required field")]
    [MaxLength(50, ErrorMessage = "To long")]
    [DisplayName("Name")]
    public string Name { get; set; }
    [Required(ErrorMessage = "required field")]
    [MaxLength(50, ErrorMessage = "To long")]
    [DisplayName("Firstname")]
    public string FirstName { get; set; }
    [Required(ErrorMessage = "required field")]
    [Range(1,110 ,ErrorMessage = "To long")]

    public string Age { get; set; }
    [Required(ErrorMessage = "required field")]
    [EmailAddress(ErrorMessage ="Not a valid email address")]
    public string Email { get; set; }
    [Required(ErrorMessage = "required field")]
    [DisplayName("slot1")]
    public int Slot1SessionId { get; set; }
    [Required(ErrorMessage = "required field")]
    [DisplayName("slot2")]
    public int Slot2SessionId { get; set; }
    [Required(ErrorMessage = "required field")]
    [DisplayName("slot3")]
    public int Slot3SessionId { get; set; }


    private List<bool> _devices;

    public List<bool> Devices
    {
        get { return _devices; }
        set {_devices = value; }
    }

    [Required(ErrorMessage = "required field")]
    [DisplayName("Organization")]
    public int OrganizationId { get; set; }
    public bool ClosingParty { get; set; }



}
Run Code Online (Sandbox Code Playgroud)

我的观点是这样的:

        @foreach (Device d in Data.GetDevices())
    {
        <label asp-for="NewRegistration.Devices">@d.Name</label>
        <input asp-for="NewRegistration.Devices" class="checkbox" type="checkbox" />
    }
Run Code Online (Sandbox Code Playgroud)

Cod*_*shi 6

错误原因

你的错误是因为你有这个:asp-for="NewRegistration.Devices"。这是无效的,因为标签是复选框类型的输入,因此asp-for需要一个布尔值,而您正在向它发送一个布尔值列表。

解决方案

这将为您的设备创建复选框:

@foreach (Device d in Data.GetDevices())
{
    @Html.CheckBox(item);
}
Run Code Online (Sandbox Code Playgroud)

但我的假设是您希望用户选择这些复选框,然后将它们发回给您,如果是这种情况,那么您需要执行以下操作:

您需要创建这样的模型:

public class Device
{
    public string Name { get; set; }
}
public class DevicesCollectionModel
{
    public List<Device> AvailableDevices { get; set; }
    public List<Device> SelectedDevices { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

填充DevicesCollectionModel.AvailableDevices并将其传递给您的视图。然后在您的视图中,为每个设备创建一个复选框AvailableDevices。注意name下面输入标签的属性:

@foreach (var item in Model.AvailableDevices)
{
<div class="checkbox">
    <label>
        <input type="checkbox"
                name="SelectedDevices"
                value="@item.Name" /> @item.Name
        </label>
    </div>
}
Run Code Online (Sandbox Code Playgroud)

然后在您的控制器中,在 post 中,您将需要访问该SelectedDevices属性以找出用户选择了什么。

此外,您可以创建单独的模型:一个用于发送给用户,另一个由控制器从用户接收。您传递给视图AvailableDevices的模型可以只有 ,而您在控制器中获得的模型可以具有SelectedDevices。MVC 将使用name输入标签中的属性为您绑定它。