Asp.net mvc 4 MVC复选框列表绑定到模型

Asp.net mvc 4 MVC复选框列表绑定到模型,asp.net-mvc-4,asp.net-core-mvc,Asp.net Mvc 4,Asp.net Core Mvc,我正在尝试收集用户为复选框列表选择的所有选项。复选框列表是使用foreach循环构建的,我有一个int[]试图将id放入其中。任何帮助都会很好 看法 @{ int idxFormats=0; foreach(Model.ListOfFormats中的var项) { @Html.Raw(@item.Name) @HiddenFor(m=>Model.selectedFormats[idxFormats]); idxFormats++; } @Html.ValidationMessageFor(mo

我正在尝试收集用户为复选框列表选择的所有选项。复选框列表是使用foreach循环构建的,我有一个int[]试图将id放入其中。任何帮助都会很好

看法

@{
int idxFormats=0;
foreach(Model.ListOfFormats中的var项)
{
@Html.Raw(@item.Name)
@HiddenFor(m=>Model.selectedFormats[idxFormats]);
idxFormats++;
}
@Html.ValidationMessageFor(model=>model.selectedFormats[idxFormats])
}
模型

公共列表列表格式{get;set;}
[显示(Name=“必须至少选择一种‘格式’)]
public int[]selectedFormats{get;set;}

如果希望将id作为IDX格式的值,请在复选框中使用此代码:

<input type="checkbox" value=@item.Value name="chkFormat" id="@idxFormats" />

编辑:

我对c不太熟悉,我用最少的代码进行了测试:

// Replace the model correct path by yours
@model IEnumerable<WebApplication1.Models.MyModels.ListOfFormats>


@{
    int idxFormats = 0;
    foreach (var item in Model)
    {
        <div class='col-md-6'>
            <input type="checkbox" value=@item.Value name="chkFormat" id="@idxFormats"/>
            <label>@Html.Raw(@item.Name)</label>
            
        </div>
        idxFormats++;
    }
    
}
//将模型正确路径替换为您的路径
@模型IEnumerable
@{
int idxFormats=0;
foreach(模型中的var项目)
{
@Html.Raw(@item.Name)
idxFormats++;
}
}

将复选框名称更改为
selectedFormats

<input type="checkbox" value=@item.Value name="selectedFormats" />

测试示例:

型号:

public class Test
{
    public List<GenericValues> ListOfFormats { get; set; }
    public int[] selectedFormats { get; set; }
}

public class GenericValues
{
    public int Value { get; set; }
    public string Name { get; set; }
    public bool Selected { get; set; }
}
公共类测试
{
公共列表ListOfFormats{get;set;}
public int[]selectedFormats{get;set;}
}
公共类泛型值
{
公共int值{get;set;}
公共字符串名称{get;set;}
已选择公共布尔值{get;set;}
}
视图:

@模型测试
@{
ViewData[“标题”]=“索引”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
指数
@{
foreach(Model.ListOfFormats中的var项)
{
@Html.Raw(@item.Name)
}
}
控制器:

public IActionResult Index()
{
    Test test = new Test
    {
        ListOfFormats = new List<GenericValues>
        {
            new GenericValues
            {
                Name = "A",
                Value = 1,
            },
            new GenericValues
            {
                Name = "B",
                Value = 2,
            },
            new GenericValues
            {
                Name = "C",
                Value = 3,
            }
        }
    };
    return View(test);
}

[HttpPost]
public IActionResult Index(Test test)
{

    return Ok();
}
public IActionResult Index()
{
测试=新测试
{
ListOfFormats=新列表
{
新的通用值
{
Name=“A”,
值=1,
},
新的通用值
{
Name=“B”,
值=2,
},
新的通用值
{
Name=“C”,
值=3,
}
}
};
返回视图(测试);
}
[HttpPost]
公共IActionResult索引(测试)
{
返回Ok();
}
结果:


您想使用IDX格式作为id还是使用@item.id之类的其他格式?在ListOfFormats的这个模型上,我没有id。我可以使用IDX格式并将值设置为item.value吗?您在找这个吗
id=“@idxFormats”
我应该在哪里添加它?我仍然得到所有的0。在控制器中(不是我…:)让我编辑…@Jefferson它现在为您工作了吗?否选中的复选框值需要发送到控制器,而不是显示在psge上
public class Test
{
    public List<GenericValues> ListOfFormats { get; set; }
    public int[] selectedFormats { get; set; }
}

public class GenericValues
{
    public int Value { get; set; }
    public string Name { get; set; }
    public bool Selected { get; set; }
}
@model Test

@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Index</h1>

<form method="post">
    @{
        foreach (var item in Model.ListOfFormats)
        {
            <div class='col-md-6'>
                <input type="checkbox" value=@item.Value name="selectedFormats" />
                <label asp-for=@item.Selected>@Html.Raw(@item.Name)</label>
            </div>
        }
    }
    <input type="submit" value="submit" />
</form>
public IActionResult Index()
{
    Test test = new Test
    {
        ListOfFormats = new List<GenericValues>
        {
            new GenericValues
            {
                Name = "A",
                Value = 1,
            },
            new GenericValues
            {
                Name = "B",
                Value = 2,
            },
            new GenericValues
            {
                Name = "C",
                Value = 3,
            }
        }
    };
    return View(test);
}

[HttpPost]
public IActionResult Index(Test test)
{

    return Ok();
}