在ASP.NET Core MVC中,如果你想绑定一个复选框列表,你可以按照以下步骤进行操作:
ProductViewModel
视图模型,其中有一个Categories
属性来表示产品的分类:public class ProductViewModel
{
public List Categories { get; set; }
}
Html.CheckBox
或Html.CheckBoxFor
来生成复选框列表。你可以使用foreach
循环来遍历分类列表,并为每个分类生成一个复选框。同时,你需要在每个复选框的name
属性中使用索引来确保将值传递回控制器:@model ProductViewModel
@foreach (var category in Model.Categories)
{
@category
}
ProductViewModel
作为参数,并使用模型绑定来自动将复选框列表的值绑定到Categories
属性:[HttpPost]
public IActionResult Create(ProductViewModel model)
{
// 处理复选框列表的值
List selectedCategories = model.Categories;
// 其他逻辑...
return RedirectToAction("Index");
}
这样,你就可以在ASP.NET Core MVC中成功绑定复选框列表的值了。