My controller is as follows
public ActionResult Create()
{
ViewBag.ClubList = GetClubs();
return View();
}
public static List<SelectListItem> GetClubs()
{
List<SelectListItem> ls = new List<SelectListItem>();
var club = new ClubsService().RecordView().ToList();
foreach (var item in club)
{
ls.Add(new SelectListItem { Text = item.clubID.Trim(), Value = item.clubID.Trim() });
}
return ls;
}
And my View is as follows
@Html.DropDownListFor(model => model.clubID, ViewBag.ClubList, new { @style = "width:400px; text-align: left", @class = "btn btn-default dropdown-toggle" })
This is generating an error
('HtmlHelper' does not contain a definition for 'DropDownListFor' and the best extension method overload 'SelectExtensions.DropDownListFor(HtmlHelper, Expression>, IEnumerable, object)' has some invalid arguments.
ClubID comes from the Clubs table where the model I am populating comes from the products table.
Can someone send me in the right direction. Bit new to MVC.
Thanks.
i use another way to prevent loop and get expected result
var club = new ClubsService().RecordView().ToList();
ViewBag.ClubList = new SelectList(club, "clubID", "clubID");
first clubID
defines the value and second clubID
defines the text, i used both clubID
Because in your example you used item.clubID
and in view
@Html.DropDownListFor(model => model.clubID, (SelectList)ViewBag.ClubList,"-- Select Clubs --", new { @style = "width:400px; text-align: left", @class = "btn btn-default dropdown-toggle" })