Asp.net mvc 5 如何生成IEnumerable并在controller/Edit CudtomerName和CustomerUrName中设置Text属性

Asp.net mvc 5 如何生成IEnumerable并在controller/Edit CudtomerName和CustomerUrName中设置Text属性,asp.net-mvc-5,Asp.net Mvc 5,是否有人知道如何生成IEnumerable并在controller/Edit CudtomerName和CustomerSurname中设置Text属性,如下所示 在付款/创建控制器中,我替换控制器 ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS, "CUSTID", "CustomerName"); 为此: ViewBag.PaymentCustomer = db.CUSTOMERS.ToList().Select(c =&

是否有人知道如何生成IEnumerable并在controller/Edit CudtomerName和CustomerSurname中设置Text属性,如下所示

在付款/创建控制器中,我替换控制器

ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS, "CUSTID", "CustomerName");
为此:

    ViewBag.PaymentCustomer = db.CUSTOMERS.ToList().Select(c => new SelectListItem
    {
        Value = c.CUSTID.ToString(),
        Text = string.Format("{0} {1}", c.CustomerName, c.CustomerSurname)
    });
以及它的工作原理。但在付款/编辑中:

ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS, "CUSTID", "CustomerName", pAYMENT.PaymentCustomer);
两个ViewBag.PaymentCustomer看起来都一样。Payment/编辑它的第四个参数也是Payment.PaymentCustomer。我无法在db.CUSTOMERS.ToList中使用pAYMENT.PaymentCustomer。选择

我试试这个:

在构建和运行之后。我可以看到下拉列表,内部下拉列表不显示Customername和CustomerUrName。它显示System.Web.Mvc.SelectListItem

我该怎么做呢?

如果你想生成IEnumerable意味着只需访问以下链接,它将对你有所帮助


这与公认的答案有何不同?我在上一个问题中解释过,在“创建”视图中工作,但在“编辑”视图中不工作。ViewBag.PaymentCustomer参数在两个视图中都不同。什么不起作用?不清楚你的问题是什么。Stephen我在问题中详细解释了。因为它需要是…},Value,Text,pAYMENT.PaymentCustomer;这不是我的建议,它是可怕的和毫无意义的额外开销!它只是ViewBag.CustomerList=db.CUSTOMERS.Selectc=>newselectListItem{Value=c.CUSTID.ToString,Text=string.Format{0}{1},c.CustomerName,c.customerUrName;
        //ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS, "CUSTID", "CustomerName", pAYMENT.PaymentCustomer);
        ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS.ToList().Select(c => new SelectListItem
        {
            Value = c.CUSTID.ToString(),
            Text = string.Format("{0} {1}", c.CustomerName, c.CustomerSurname)
        }), pAYMENT.PaymentCustomer);
    ViewBag.CustomerList = new SelectList(db.CUSTOMERS.ToList().Select(c => new SelectListItem {
        Value = c.CUSTID.ToString(),
        Text = string.Format("{0} {1}", c.CustomerName, c.CustomerSurname),
    }), "Value", "Text", pAYMENT.PaymentCustomer);