簡體   English   中英

從收音機切換到Checkboxfor

[英]Switching Model from Radio to 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實際上返回為空(與SelectedLeader相同,但不使用該ATM)。 我相信我設置CheckboxFor的方式可能有問題,但是它確實正確設置了其初始設置。

有任何想法嗎? 謝謝你

史蒂夫

安德烈是正確的。 它似乎不適用於IEnumerable(甚至使用ElementAt(i))-我不得不將ViewModel更改為IList,現在它表現得很棒。 真正感謝您的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM