【发布时间】:2014-05-21 04:23:40
【问题描述】:
我正在使用 HTML CheckBoxListFor 扩展来显示带有复选框的列表并检索选定的值。问题是我在控制器中得到空值。
查看代码
@Html.CheckBoxListFor(model => model.PostedStyles, model => model.styles, style => style.Value,style =>style.Text, model => model.selStyles,Position.Horizontal)
视图模型
public IEnumerable<CheckboxStyle> styles { get; set; }
public IEnumerable<CheckboxStyle> selStyles { get; set; }
public PostedStyles PostedStyles { get; set; }
类
public class CheckboxStyle
{
public bool IsSelected { get; set; }
public int Value { get; set; }
public string Text { get; set; }
}
public class PostedStyles
{
public string[] StyleIds { get; set; }
}
控制器代码:
初始加载
public ActionResult Index()
{
return View(GetStylesInitialModel());
}
private CreateLabelViewModel GetStylesInitialModel()
{
//setup properties
var model = new CreateLabelViewModel();
var selectedFruits = new List<CheckboxStyle>();
//setup a view model
model.styles = StyleRepository.GetAll().ToList();
model.selStyles = selectedFruits;
return model;
}
点击按钮
[HttpPost]
public ActionResult GenerateLabels(PostedStyles postedStyles)
{
return View(GetStylesModel(postedStyles));
}
我在上面的postedStyles 中得到空值。
检查了很多,但没有得到任何相关的东西。请指教。
【问题讨论】:
-
它在 CheckBoxStyle 类中 public bool IsSelected { get;放; }
标签: asp.net-mvc-4