我的班级
public partial class Team
{
public int TeamId { get; set; }
public string TeamName { get; set; }
public string TeamDescription { get; set; }
public virtual IList<Trials> Trials { get; set; }
}
试验是无关紧要的
public partial class Trials
{
public int TrialID { get; set; }
public string Name { get; set; }
public int TrialTyp_RefID { get; set; }
public bool isChk { get; set; }
public virtual ICollection<Team> Team { get; set; }
}
在我看来,我正在尝试将ListBoxFor与团队中的Trials绑定
@model Trials.Classes.Team
<td colspan="2">
@Html.ListBoxFor(model=> model.Trials,
new SelectList(ViewBag.trials,"TrialID", "Name"),
new { @class = "chosen-select", data_placeholder = "Select Trials...", style = "width:500px;", tabindex = "4" }
)
</td>
我无法在控制器中获得Trials calss的任何值。它显示为null但我从列表框中选择了多个值
答案 0 :(得分:1)
多重选择仅回发原始值数组。它不会回发一组复杂的对象。
您需要具有属性的视图模型来绑定选定的Trials
public class TeamVM
{
public int TeamId { get; set; }
....
public int[] SelectedTrials { get; set; }
public SelectList Trials { get; set; } // Assign this in your controller rather than using ViewBag
}
然后在你看来
@Html.ListBoxFor(m => m.SelectedTrials, Model.Trials)
当您回发时,Team.SelectedTrials
将包含所选TrialID
值的数组。
答案 1 :(得分:0)
我将Team类更改为
public partial class Team
{
public int TeamId { get; set; }
public string TeamName { get; set; }
public string TeamDescription { get; set; }
public virtual IList<Trials> Trials { get; set;
public int[] AuthorisedTrials { get; set; }
}
并在视图中
@model Trials.Classes.Team
<td colspan="2">
@Html.ListBoxFor(model=> model.AuthorisedTrials ,
new SelectList(ViewBag.trials,"TrialID", "Name"),
new { @class = "chosen-select", data_placeholder = "Select Trials...", style = "width:500px;", tabindex = "4" }
)
</td>
在我的控制器中,我能够获得选定试验的Vlaues