我已经尝试了许多不同的方法将所选项目传递到多选列表而没有运气.最后,我尝试了这个,我认为应该显示所有选中的项目,但仍然没有选择列表中的任何内容.
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>