MVC3 View中的CheckboxList,获取传递给控制器的选中项 [英] CheckboxList in MVC3 View and get the checked items passed to the controller
问题描述
我有一个关于更多信息的课程:
public class MoreInfo{公共字符串名称 { 获取;放;}公共字符串 selectedCheckboxItems {get;放;}}
我想知道如何在视图上创建一个复选框列表,并在提交时将选中的项目传递给我的控制器.
我将如何创建复选框列表以及如何传递所有选中的项目并处理它们?
让我们稍微修改一下你的模型:
公共类ItemViewModel{公共字符串 ID { 获取;放;}公共字符串名称 { 获取;放;}公共布尔检查{得到;放;}}
那么你可以有一个控制器:
公共类 HomeController:控制器{公共 ActionResult 索引(){//这个动作用来渲染表单 =>//我们应该用一些值填充我们的模型//这显然可能来自某个数据源var 模型 = 新 []{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" },};返回视图(模型);}[HttpPost]公共 ActionResult 索引(IEnumerable 项){//提交表单时将调用此操作//在这里视图模型将被正确绑定和//您将获得所有项目的集合//对应的 id、name 以及是否被检查...}}
然后您将有一个相应的视图 (~/Views/Home/Index.cshtml
),其中包含允许用户检查/取消检查值的表单:
@model IEnumerable@using (Html.BeginForm()){@Html.EditorForModel()<input type="submit" value="OK"/>}
最后是编辑器模板(~/Views/Home/EditorTemplates/ItemViewModel.cshtml
):
@model AppName.Models.ItemViewModel//这两个隐藏字段只是为了持久化id和name@Html.HiddenFor(x => x.Id)@Html.HiddenFor(x => x.Name)<div>@Html.CheckBoxFor(x => x.Checked)@Html.LabelFor(x => x.Checked, Model.Name)
I have a class for MoreInfo:
public class MoreInfo
{
public string Name { get; set; }
public string selectedCheckboxItems {get; set;}
}
I want to know how to create a checkbox list on the view and pass the checked off items to my controller on submit.
How would I go about creating the checkbox list and how to pass all the checked items and process them?
Let's modify your model a little:
public class ItemViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
}
then you could have a controller:
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
...
}
}
then you would have a corresponding view (~/Views/Home/Index.cshtml
) which would contain the form allowing the user to check/uncheck values:
@model IEnumerable<AppName.Models.ItemViewModel>
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="OK" />
}
and finally the editor template (~/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>
这篇关于MVC3 View中的CheckboxList,获取传递给控制器的选中项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!