0

Does anyone knows how to generate IEnumerable and set the Text property in controller/Edit CudtomerName and CustomerSurname as shown below?

In Payment/Create controller I replace;

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

with this:

    ViewBag.PaymentCustomer = db.CUSTOMERS.ToList().Select(c => new SelectListItem
    {
        Value = c.CUSTID.ToString(),
        Text = string.Format("{0} {1}", c.CustomerName, c.CustomerSurname)
    });

and its working. But in Payment/Edit is:

ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS, "CUSTID", "CustomerName", pAYMENT.PaymentCustomer);

Both ViewBag.PaymentCustomer looks same. The Payment/Edit its also get 4th parameter as "pAYMENT.PaymentCustomer". I cannot use "pAYMENT.PaymentCustomer" in db.CUSTOMERS.ToList().Select...

I try this:

        //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);

And After build and run. I can see dropdown list and inside drop down list does not shows Customername and CustomerSurname. It shows "System.Web.Mvc.SelectListItem".

How do I do that?

NTMS
  • 816
  • 7
  • 22
  • How does this differ from [your last question](http://stackoverflow.com/questions/33854197/how-to-send-customername-and-customersurname-from-controller-to-edit-or-create-c) and the accepted answer? –  Nov 22 '15 at 21:16
  • I explained in last question that in create view is worked but not the edit view. The "ViewBag.PaymentCustomer " parameter is different in both view. – NTMS Nov 23 '15 at 16:43
  • What is not working? Its not clear what your problem is. –  Nov 23 '15 at 23:20
  • Stephen I explained in detail in my question. – NTMS Nov 24 '15 at 07:29
  • Because it needs to be `...}), "Value", "Text", pAYMENT.PaymentCustomer);` –  Nov 24 '15 at 07:40
  • But this is an awful way of generating your dropdownlist. Simply use `ViewBag.CustomerList = db.CUSTOMERS.ToList().Select(c => new SelectListItem { ..... }); and in the view use `@Html.DropDownListFor(m => m.PaymentCustomer, (IEnumerable)ViewBag.CustomerList)` –  Nov 24 '15 at 07:44
  • Can you explain in details. Because I believe I set the value and text in db.CUSTOMERS.ToList().Select... !!! – NTMS Nov 24 '15 at 07:45
  • Yes you did, but then your creating another (pointless) `SelectList` from the first `SelectList` but not setting the `Value` and `Text` properties of the second `SelectList` –  Nov 24 '15 at 07:47
  • Then where I put "pAYMENT.PaymentCustomer". I did what you said and get errors. – NTMS Nov 24 '15 at 07:47
  • I have already shown where to put `pAYMENT.PaymentCustomer` - it comes after `"Text",` And what errors (we are not psychic). But read my other comment and do this correctly and strongly bind to your model property using `DropDownListFor()` –  Nov 24 '15 at 07:50
  • I got it. My problem was on the "Value", "Text". I forget to put the double punctuation mark. Thank you very much. – NTMS Nov 24 '15 at 08:06

2 Answers2

0

If u want to generate IEnumerable<SelectListItem> means Just visit the following link it will helpful for u

Asp.Net MVC with Drop Down List, and SelectListItem Assistance

http://codeclimber.net.nz/archive/2009/08/05/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx

Community
  • 1
  • 1
Vinoth
  • 851
  • 7
  • 23
0
    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);
NTMS
  • 816
  • 7
  • 22
  • This is NOT my suggestion and its awful and pointless extra overhead! Its just `ViewBag.CustomerList = db.CUSTOMERS.Select(c => new SelectListItem { Value = c.CUSTID.ToString(), Text = string.Format("{0} {1}", c.CustomerName, c.CustomerSurname);` –  Jan 17 '16 at 12:26