【发布时间】:2012-03-26 00:41:22
【问题描述】:
我有以下 ViewModel
public class RecommendationModel
{
public List<CheckBoxItem> CheckBoxList { get; set; }
}
public class CheckBoxItem
{
public string Text { get; set; }
public bool Checked { get; set; }
public string Link { get; set; }
}
用下面的查看
model Sem_App.Models.RecommendationModel
@using (Html.BeginForm())
{
for (int i = 0; i < Model.CheckBoxList.Count(); i++) {
@Html.CheckBoxFor(m => m.CheckBoxList[i].Checked)
@Html.DisplayFor(m => m.CheckBoxList[i].Text)
}
<input type="submit" value="Add To Playlist" />
}
使用以下控制器操作
//get
public ActionResult Recommendation()
{
RecommendationModel model = new RecommendationModel();
model.CheckBoxList = new List<CheckBoxItem>();
return PartialView(model);
}
//post
[HttpPost]
public ActionResult Recommendation(RecommendationModel model)
{
foreach (var item in model.CheckBoxList)
{
if (item.Checked)
{
// do something with item.Text
}
}
}
问题是每当我选择一些项目并按下提交按钮时,模型返回的 CheckBoxList 为空。如何更改视图以返回 CheckBoxList 列表?试
@Html.HiddenFor(m => m.checkBoxList) 对我不起作用
【问题讨论】:
标签: asp.net-mvc-3 model controller