复选框和模型绑定的动态列表 [英] Dynamic list of checkboxes and model binding
问题描述
我正在尝试创建一个视图,其中包含从数据库动态创建的复选框列表,然后在表单发回时检索所选的列表。我的EF模型包含一个类:
public class ItemIWouldLikeACheckboxFor {
public int Id {get;组; }
public string描述{get;组;
}
我有一个视图模型,其中包含以下列表:
public class PageViewModel {
//各种其他属性
public List< ItemIWouldLikeACheckboxFor> checkboxList {get;组;
}
我的控制器获取方法:
public ActionResult Create(){
var viewModel = new PageViewModel();
viewModel.checkboxList = db.ItemIWouldLikeACheckboxFors.ToList();
return View(viewModel);
}
我的观点:
<%using(Html.BeginForm()){%>
<% - 其他内容...%>
<%foreach(checkboxList中的var item){%>
<%:Html.CheckBox(<! - 究竟是什么 - >)%>
<%}%>
<% - 其他的东西在这里...%>
< input type =submit/>
<%}%>
我的控制器帖子方法:
[HttpPost]
public ActionResult Create(PageViewModel viewModel){
//与其他字段进行对象
//我想做某事如:
foreach(selectedCheckBoxes中的var item){
// do stuff
}
}
我似乎无法让它工作。我的基本问题作为代码片段中的注释混合在一起,但是要概述:
- 我的视图模型是否正常? (我需要添加任何东西才能捕获所选的对象,而不是简单地显示列表)。
- 我们应该在视图中放置什么来渲染每个复选框? >
- 如何在发布后访问控制器中的所选复选框?
您是否看过: http: //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx ?
我们基本上写了我们自己的控制来渲染HTML,如
< label for =产品>选择产品< / label>
< ul class =checkBoxList>
< li>
< input type =hiddenvalue =0name =Products.Index>
< input type =checkboxvalue =3424name =Products [0] .Idid =Products0>
< label for =Products0> iPod touch第3代< / label>
< / li>
< li>
< input type =hiddenvalue =1name =Products.Index>
< input type =checkboxvalue =3123name =Products [1] .Idid =Products1>
< label for =Products1> Creative Zen< / label>
< / li>
< / ul>
< / div>
模型看起来好,我们写了一个自定义帮助器,所以我们的aspx页面看起来像:
<%= Html.DropDownFor(m => m.products)%>
如果您关注phil haacks发布,您的模型应该自动绑定到控制器中。
I'm trying to create a view that contains a list of checkboxes that is dynamically created from a database, and then retrieve the list of selected ones when the form is posted back.
My EF model contains a class:
public class ItemIWouldLikeACheckboxFor {
public int Id { get; set; }
public string Description { get; set; }
}
I have a view model that contains a list of these:
public class PageViewModel {
// various other properties
public List<ItemIWouldLikeACheckboxFor> checkboxList { get; set; }
}
My controller get method:
public ActionResult Create() {
var viewModel = new PageViewModel();
viewModel.checkboxList = db.ItemIWouldLikeACheckboxFors.ToList();
return View(viewModel);
}
My view:
<% using (Html.BeginForm()) { %>
<%-- other stuff here... %>
<% foreach (var item in checkboxList) { %>
<%: Html.CheckBox( <!-- what exactly ?????? -->) %>
<% } %>
<%-- other stuff here...%>
<input type="submit" />
<% } %>
My controller post method:
[HttpPost]
public ActionResult Create(PageViewModel viewModel) {
// do stuff with other fields
// I would like to do something like:
foreach (var item in selectedCheckBoxes) {
// do stuff
}
}
I can't seem to get it to work. My basic questions are mixed in as comments in the code snippets, but to recap:
- Is my view model OK? (do I need to add anything to capture the selected ones as opposed to simply the list to display?)
- What exactly should I put in the view to render each check box?
- How do I access the selected checkboxes in the controller after the post?
Have you seen: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx?
We basically wrote our own control to render the HTML like
<label for="Products"> Select Products </label>
<ul class="checkBoxList">
<li>
<input type="hidden" value="0" name="Products.Index">
<input type="checkbox" value="3424" name="Products[0].Id" id="Products0">
<label for="Products0">iPod touch 3rd Generation</label>
</li>
<li>
<input type="hidden" value="1" name="Products.Index">
<input type="checkbox" value="3123" name="Products[1].Id" id="Products1">
<label for="Products1">Creative Zen</label>
</li>
</ul>
</div>
Model Looks Ok, we wrote a custom helper, so our aspx pages look like:
<%= Html.DropDownFor(m=>m.products) %>
If you follow phil haacks post, your model should automatically bind in your controller.
这篇关于复选框和模型绑定的动态列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!