[RESOLVED]drop down list in mvc 5

Im reading from database into a drop down list here is my code

the controller:

public ActionResult Index()
{
var data = from p in db.categories

select new
{
category = p.categoryName

};

SelectList list = new SelectList(data);
ViewBag.Roles = list;
return View();
}

the view:

category<br />
@Html.DropDownList("category", ViewBag.Roles as SelectList)
<br/>

there seem to be a problem because the display is as follows:

{ category = ping pong } instead if ping pong

why is that?can anyone edit my code?? I’ll appreciate any help 

problem solved:

public ActionResult Index()
{
var data = from p in db.categories

select p.categoryName;

SelectList list = new SelectList(data);
ViewBag.Roles = list;
return View();
}

and in the view

category<br />
@Html.DropDownList("category", ViewBag.Roles as SelectList)
<br/>

Leave a Reply