【发布时间】:2017-01-20 15:20:15
【问题描述】:
您好,我正在尝试从多选下拉框中选择客户 ID 的客户文件;我遇到上述错误的控制器
public ActionResult Create(CountryViewModel country)
{
List<SelectListItem> selectedItems = country.MenuCountry.Where(p => country.ContryIds.Contains(int.Parse(p.Value))).ToList();
try
{
CustomerClient CC = new CustomerClient();
var result = CC.findAll();
var matches = from customer in result
where selectedItems.Contains(customer.Id) // Here is error and saying cannot convert from 'int' to 'system.web.ui.webcontrols.listitem
select customer;
// ViewBag.listCustomers = CC.findAll();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
我的客户类在下面给出
public class Customer
{
[Display(Name = "CustomerId")]
public int Id { get; set; }
[Display(Name = "Country")]
public string Country { get; set; }
}
请指教;非常感谢
【问题讨论】:
-
selectedItems需要是List<int>类型(或int[]等) -
selectedItems.Any(s=> s.value==ccustomer.Id.ToString())
-
@MarkC。在 asp.net mvc 中,我们有一个类 SelectListItem 。我认为用户正在使用它来创建下拉列表。它有一个 Text 和 value 属性都是字符串类型
-
嗨,我的 countryViewMovel 如下所示 public class CountryViewModel { public List
MenuCountry{ get;放; } 公共 int[] ContryIds { 获取;放; } } -
@user3624511 试试我的回答让我知道这是否是你想要的。
标签: c# asp.net linq model-view-controller drop-down-menu