【问题标题】:HTML CheckBoxListFor in MVC4 returning null valuesMVC4 中的 HTML CheckBoxListFor 返回空值
【发布时间】: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 中得到空值。

检查了很多,但没有得到任何相关的东西。请指教。

【问题讨论】:

标签: asp.net-mvc-4


【解决方案1】:

你可以参考以下链接:

http://mvccbl.azurewebsites.net/

希望对您有所帮助...

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2014-01-26
    • 1970-01-01
    相关资源
    最近更新 更多