2

我正在运行一个 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 的方式可能有问题,但它确实正确设置了它的初始设置。

有任何想法吗?谢谢你。

史蒂夫

4

1 回答 1

0

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

于 2014-09-24T17:11:43.190 回答