最近在我的第一个ASP.Net MVC2 Web应用程序上工作,当我需要在列表框中选择多个值时,我遇到了一些问题.我用一些jQuery解决了这个问题,但是继续编写了一些非常简单的代码来演示.我使用EF作为模型,有两个实体 - Customers和HelpDeskCalls:
控制器:
public ActionResult Edit(int id)
{
Customer currCustomer = ctx.Customers.Include("HelpDeskCalls").Where(c => c.ID == id).FirstOrDefault();
List<HelpDeskCall> currCustCalls = (ctx.HelpDeskCalls.Where(h => h.CustomerID == id)).ToList();
List<SelectListItem> currSelectItems = new List<SelectListItem>();
List<String> selectedValues = new List<string>();
foreach (HelpDeskCall currCall in currCustCalls)
{
bool isSelected = (currCall.ID % 2 == 0) ? true : false;
//Just select the IDs which are even numbers...
currSelectItems.Add(new SelectListItem() { Selected = isSelected, Text = currCall.CallTitle, Value = currCall.ID.ToString() });
//add the selected values …Run Code Online (Sandbox Code Playgroud)