将模型从无线电切换到Checkboxfor

时间:2014-09-22 23:16:48

标签: c# asp.net checkboxfor

我正在运行MVC5网站并尝试将其从使用单选按钮切换到复选框。我已经阅读了本网站上的其他几个例子,但似乎无法使其发挥作用。我当前的ViewModel作为NULL发布到控制器。这就是它目前的样子:

public class ViewModel {
    public string SelectedLeader { get;set;}
    public IEnumerable<Personnel> Team { get;set; }
}

public class Personnel : TableEntity {
    ... other attributes here
    public bool IsLead {get;set;}
}

SelectedLeader用于跟踪RadioButton的Selected元素的RowKey,因此我认为我可以将其切换到IEnumerable但与其他示例相比仍然看起来不正确。我的HTML看起来像这样:

@using (Html.BeginForm())
{
other stuff on page
    @foreach (var person in Model.Team) {
        ... print other stuff
        @Html.CheckboxFor( e => person.IsLead , new { value=@person.RowKey })
        @Html.HiddenFor(e => person.RowKey)
    }
 }

public ActionResult Edit(ViewModel viewModel) {
    ... 
}

当它回发到服务器时,Team实际上返回null(与SelectedLeader相同但不使用该ATM)。我相信我设置CheckboxFor的方式可能有问题,但它确实正确设置了初始设置。

有什么想法吗?谢谢你。

史蒂夫

1 个答案:

答案 0 :(得分:0)

安德烈是对的。它似乎不适用于IEnumerable(即使使用ElementAt(i)) - 我不得不将我的ViewModel更改为IList,现在它工作得非常棒。真的很感激帮助。