0

Note: Variables names were changed to keep my code's purpose anonymous.

Task:

Create a SelectList that will show what was chosen (if anything was chosen), and will generate a list to choose from.

ViewBag Declaration / Definition in the CarShow Controller:

/* db.Companies is DataContext (Entities), 
*  CarShow.Company.Id is a ViewModel (Company is encapsulated under CarShow) */
ViewBag.CompanyList = new SelectList( db.Companies,       //Constructor
                                      "Id",               //Property Name
                                      "Name",             //Text to display each item
                                      CarShow.Company.Id  //Value of the initially selected item
                                      ).Distinct();

I have tried the following code in my "Edit View" to generate the SelectList with its error message:

/* <-- The ViewData item that has the key 'Company.Id' is of 
    type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. --> */
@Html.DropDownListFor( model => model.Company.Id,
                      (IEnumerable<SelectListItem>)ViewBag.CompanyList )

/* <!-- Compilation Error --> */
@Html.DropDownList( (IEnumerable<SelectListItem>)ViewData.Id,
                    (IEnumerable<SelectListItem>)ViewData.Name )

/* <!-- The ViewData item that has the key 'Id' is of type 'System.Int32' 
     but must be of type'IEnumerable<SelectListItem>'. --> */
@Html.DropDownList( "Id", 
                    (IEnumerable<SelectListItem>)ViewData["Name"], 
                     new { id = "Id" })

/* <!-- Cannot apply indexing with [] to an 
     expression of type 'System.Dynamic.DynamicObject' --> */
@Html.DropDownList("Id", (IEnumerable<SelectListItem>)ViewBag["Name"])

Is my approach right and something is wrong in my "Edit View" code or my CarShow Controller and "Edit View" code wrong?

I would like to thank you in advance to anyone that helps.

2 Answers2

2

try this i do not think you want to distinct the list

ViewBag.CompanyList = new SelectList( db.Companies.Distinct().ToList(),      
                                      "Id",             
                                      "Name",             
                                      CarShow.Company.Id);


@Html.DropDownList("CompanyList", (SelectList)ViewBag.CompanyList)
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • The error I got was: DataBinding: 'System.Data.Entity.DynamicProxies.Faculty_260FAB836DB2F81BEC4B2B820E387F9BCB08D23B1F11DE5302BDE931EB440491' does not contain a property with the name 'Name'. – Aseity Architect Apr 23 '13 at 03:46
  • 1
    The reason why you get this error is because there is no property filed in you table Companies called name – COLD TOLD Apr 23 '13 at 15:30
  • I didn't know that, I thought it would change it, it works now. Thank you. – Aseity Architect Apr 23 '13 at 17:08
0

In your view use the strongly typed version:

@Html.DropDownListFor( model => model.Company.Id, (SelectList)ViewBag.CompanyList )

In your controller, remove the .Distinct() from the end of the line... and if you need it use it this way:

ViewBag.CompanyList = new SelectList( db.Companies.Distinct(), "Id",  "Name", CarShow.Company.Id );
Romias
  • 13,783
  • 7
  • 56
  • 85
  • I got this error: DataBinding: 'System.Data.Entity.DynamicProxies.Faculty_260FAB836DB2F81BEC4B2B820E387F9BCB08D23B1F11DE5302BDE931EB440491' does not contain a property with the name 'Name'. – Aseity Architect Apr 23 '13 at 03:41
  • 1
    Maybe your property is called other than "Name"? There it says "Faculty"... Does Faculty has a property named "Name"? – Romias Apr 23 '13 at 13:57
  • @Romias thanks you helped me solve an issue with your comment! I accidentally added the wrong value for`DataTextField`! – benscabbia Nov 15 '15 at 20:13