MVC3中的CheckboxList查看并获取传递给控制器​​的已检查项目

RPS*_*RPS 25 asp.net-mvc checkboxlist asp.net-mvc-3

我有一个MoreInfo类:

public class MoreInfo
{
        public string Name { get; set; }
        public string selectedCheckboxItems {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何在视图上创建一个复选框列表,并在提交时将已检查的项目传递给我的控制器.

我将如何创建复选框列表以及如何传递所有选中的项目并进行处理?

Dar*_*rov 45

让我们稍微修改你的模型:

public class ItemViewModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以有一个控制器:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        // This action is used to render the form => 
        // we should populate our model with some values
        // which could obviously come from some data source
        var model = new[]
        {
            new ItemViewModel { Id = "1", Checked = true, Name = "item 1" },
            new ItemViewModel { Id = "2", Checked = false, Name = "item 2" },
            new ItemViewModel { Id = "3", Checked = true, Name = "item 3" },
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<ItemViewModel> items)
    {
        // This action will be invoked when the form is submitted
        // and here the view model will be properly bound and
        // you will get a collection of all items with their
        // corresponding id, name and whether they were checked or not
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你会有一个相应的view(~/Views/Home/Index.cshtml),其中包含允许用户检查/取消选中值的表单:

@model IEnumerable<AppName.Models.ItemViewModel>
@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <input type="submit" value="OK" />
}
Run Code Online (Sandbox Code Playgroud)

最后是编辑模板(~/Views/Home/EditorTemplates/ItemViewModel.cshtml):

@model AppName.Models.ItemViewModel
// Those two hidden fields are just to persist the id and name
@Html.HiddenFor(x => x.Id)
@Html.HiddenFor(x => x.Name)
<div>
    @Html.CheckBoxFor(x => x.Checked)
    @Html.LabelFor(x => x.Checked, Model.Name)
</div>
Run Code Online (Sandbox Code Playgroud)


小智 21

public class MoreInfo
{
        public Int64 Id {get; set;}
        public string Name { get; set; }
        public bool Checkbox {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

控制器动作:

public ActionResult Index(){
  var list = new List<MoreInfo>{
      new MoreInfo{Id = 1, Name = "Name1", Checkbox = false},
      new MoreInfo{Id = 2, Name = "Name2", Checkbox = false},
      new MoreInfo{Id = 3, Name = "Name3", Checkbox = true},
  };
  return View(list);
}

[HttpPost]
public ActionResult Index(List<MoreInfo> lists){

  return View(lists);
}
Run Code Online (Sandbox Code Playgroud)

剃刀查看:

@model List<MoreInfo>

<form action="" method="POST">
@for (var i = 0; i < Model.Count();i++ )
{
    @Html.HiddenFor(it => it[i].Id)
    @Html.TextBoxFor(it => it[i].Name)
    @Html.CheckBoxFor(it => it[i].Checkbox)
}
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

更多信息 在这里