locked
DropdownListFor showing the object name RRS feed

  • Question

  • User1982683974 posted

    Hello

    I have a Dropdownlist populated by a list but instead of having a list of items I am getting a list of C# objects.

     @Html.CustomDropDownListFor(model => model.DisplayOwnerName, new SelectList(Model.Passengers) ,  string.Empty)

    and the List is :

     var formattedlist = items.Select(item => new SelectListItem { Text = item.FullName, Value = "Passengers" }).ToList();

    what should I change so I got the list of values


    Regards

    Tuesday, June 28, 2016 7:34 PM

Answers

All replies

  • User2053451246 posted

    Where is Model.Passengers defined and how is it defined?

    Where are you adding the SelectListItems loaded into formattedlist into Model.Passengers?

    Tuesday, June 28, 2016 8:49 PM
  • User-271186128 posted

    Hi Yosr Jemili,

    According to your code, it seems that you are using a custom DropDownList helper. If that is the case, I suggest you check the code in the CustomDropDownListFor method.

    You can refer to the following link: https://blogs.msdn.microsoft.com/stuartleeks/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums/

    Besides, I suggest you can refer to the following code to bind DropDownList:

            public ActionResult DllSetDefaultValue()
            {            
                DDLViewModel ddl = new DDLViewModel();
                ddl.SlectedValue = 3;
                ddl.Name = "AAA";
    
                var querydata = new SelectList(context.Employees.Select(c => new { EmployeeID = c.EmployeeID, FirstName = c.FirstName}), "EmployeeID", "FirstName", 3).ToList();
                ddl.CountryList = querydata;
    
                 return View(ddl);
            }

    Code in view:

         @Html.DropDownListFor(model => model.SlectedValue, Model.CountryList, "Please select", new { id = "ddlCity" })

    More information about binding DropDownList, you can check the following articles:

    http://www.c-sharpcorner.com/UploadFile/32bcb2/different-ways-for-populating-dropdownlist-in-mvc/

    http://www.c-sharpcorner.com/UploadFile/4d9083/binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data/

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 29, 2016 6:05 AM
  • User1982683974 posted

    Thank you both of you  for your answer. in fact I am just assigning to passengers the formattedlist.

    Wednesday, June 29, 2016 1:40 PM
  • User1982683974 posted

    I think my issue is a covnersion problem between selectlistitem and List since my other dropdownlists are working.

    I tried this but still I am seeing an object list :

     var items = ownerlist.Where(p => p.IpNumber < 1).Select(x => new SelectListItem { Text = x.FullName, Value = x.FullName }).ToList();
    
        @Html.CustomDropDownListFor(model => model.Vehicle.DisplayOwnerName, new SelectList(Model.Passengers))



    Friday, July 1, 2016 2:59 PM
  • User2053451246 posted

    You still aren't showing all the code that pulls what you want to happen together.  You declare a variable called "items", but the list the DropDownListFor is using is called Model.Passengers.

    Friday, July 1, 2016 6:08 PM
  • User1982683974 posted

    My dropDownList is populated with Model.passengers which has the items list.

    model.Passengers = items

    Tuesday, July 5, 2016 6:05 PM