0

I am trying to apply styling to the following line

@Html.DropDownList("productOptions", "Products")

I rewrote to the following

@Html.DropDownList("productOptions", "Products",  new { @class = "form-control" })

In my controller I added productOptions to ViewData["productOptions"] and Products is an optional string for default text

but I keep getting the following error message about the syntax

Argument 3: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>'  

Cannot resolve method 'Dropdownlist(string, string, { class:string}' candidates are:

Anyhelp is greatly appreciated.

Kirk
  • 16,182
  • 20
  • 80
  • 112
user1250264
  • 897
  • 1
  • 21
  • 54
  • Possible duplicate of [Populating a dropdown from ViewData](http://stackoverflow.com/questions/12090937/populating-a-dropdown-from-viewdata) – CodeArtist Dec 28 '15 at 20:49

2 Answers2

0

"Products" needs to be the actual object not a string.

Example usage:

public class ExampleClass //This is the model class
{
    public string SelectedProvider { get; set; }
    public ICollection<System.Web.Mvc.SelectListItem> Providers { get; set; }
}
//The above class you define it this way in your view
@model ExampleClass

//And you use it like this.
@Html.DropDownListFor(model => model.SelectedProvider, Model.Providers, new { @class = "form-control" })

//In your case you have your data in ViewData
@Html.DropDownList("Products", 
new SelectList((IEnumerable) ViewData["productOptions"], "Id", "Name"))

For more information look here: Populating a dropdown from ViewData

Community
  • 1
  • 1
CodeArtist
  • 5,534
  • 8
  • 40
  • 65
0

Try this one

List<Product> tempProduct = new List<Product>();
tempProduct = context.Products.ToList();
ViewData["tempProduct"] = tempProduct ;

@Html.DropDownList("Select Product",  new SelectList((IEnumerable) ViewData["tempProduct"], "Id", "Name" , new { @class = "form-control" }))
Deepak
  • 121
  • 5