我尝试了许多不同的方法将所选项目传递给多选列表,但没有运气。最后,我尝试了这个,我认为它应该显示所有已选择的项目,但列表中仍然没有选择任何内容。
public MultiSelectList Companies { get; private set; }
Companies = MulitSelectList(subcontractRepository.SubcontractCompanies(Subcontract.subcontract_id), "Value", "Text");
在 SubcontractRepository.cs 中:
public IEnumerable<SelectListItem> SubcontractCompanies(Guid id)
{
return c in db.companies
select new SelectListItem
{
Text = c.company_name,
Value = c.company_id.ToString(),
Selected = true
}
}
在视图中:
<p>
<label for="Companies">Company:</label>
<%= Html.ListBox("Companies", Model.Companies) %>
<%= Html.ValidationMessage("Companies", "*") %>
</p>