asp.net mvc - Error displaying database values in dropdown box in c# -


I have a problem on how to display a dropdown list of values ​​in my database in my database table (Categories) There are 2 columns, which are category IDs and titles. The goal is to provide a dropdown list where a user can select the title stored in the table.

Model:

  received a public partial category category {Public Entity}; Set; } Public string title {get; Set; }}   

Controller:

  Public Action Index () {Private Reference DB = New Context (); ViewBag.Category = new selection list (db categories, "id", "title"); See Return (); }   

See:

  @ Model MyApp.Models.Category & lt; P & gt; Categories: @ Html.DropDownList ("ID", "Choose a Category") & lt; / P & gt;   

The error I get is in the scene that says:

  'IEnumerable & lt; SelectListItem & gt; ' There is no ViewData item of type, whose key is 'ID'    

strong> viewbag and you see that the "id" is going through you class

  ViewBag.Category = new SelectList (db.Categories, "id", "title");   

Try this type:

  @ html.DropDownList ("category", "select a category")    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -