0

I created list of objects in controller and I would like show one of property in DropDownList. This is my controller:

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<Product> product = new List<Product>();

            product.Add(new Product() { ProductId = 1, Name = "apple", Price = 1.09 });
            product.Add(new Product() { ProductId = 2, Name = "peach", Price = 1.57 });
            product.Add(new Product() { ProductId = 3, Name = "banana", Price = 1.15 });
            product.Add(new Product() { ProductId = 4, Name = "watermelon", Price = 4.50 });
            product.Add(new Product() { ProductId = 5, Name = "melon", Price = 5.06 });
            product.Add(new Product() { ProductId = 6, Name = "strawberries", Price = 6.99 });
            product.Add(new Product() { ProductId = 7, Name = "raspberries", Price = 2.20 });

            return View(product);
        }
    }

And I try use it into my View below but it return false. How can I use helper @Html.DropDownListFor?

@foreach(var name in Model.Name)
    { 
        @Html.DropDownListFor(name.Name ,name.ToString())
    }
kenzolek
  • 344
  • 6
  • 24
  • first off, this wouldn't compile because there isn't an object named `produkt` – Grizzly Jun 17 '16 at 13:53
  • Possible duplicate of [MVC3 DropDownListFor - a simple example?](http://stackoverflow.com/questions/7142961/mvc3-dropdownlistfor-a-simple-example) – Owen Pauling Jun 17 '16 at 13:55
  • you are just looking to create a dropdown with all the values in the `product` list as options? – SOfanatic Jun 17 '16 at 13:56
  • is `Product` a table in your database? – Grizzly Jun 17 '16 at 13:58
  • To use `DropDownListFor()` you need a property to bind to (say `int SelectedProduct`) and a collection. You model is a collection only. Suggest you refer [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for how to do this correctly. –  Jun 17 '16 at 23:33

2 Answers2

2

I am assuming that for the Product class looks like:

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}

And let us say that you have the following model:

public class ProductModel 
{
   public int MyProductId { get; set; }
}

And finally you have created the list of Products (like you have shown)

List<Product> product = new List<Product>();

            produkt.Add(new Product() { ProductId = 1, Name = "apple", Price = 1.09 });
            produkt.Add(new Product() { ProductId = 2, Name = "peach", Price = 1.57 });
            produkt.Add(new Product() { ProductId = 3, Name = "banana", Price = 1.15 });
            produkt.Add(new Product() { ProductId = 4, Name = "watermelon", Price = 4.50 });
            produkt.Add(new Product() { ProductId = 5, Name = "melon", Price = 5.06 });
            produkt.Add(new Product() { ProductId = 6, Name = "strawberries", Price = 6.99 });
            produkt.Add(new Product() { ProductId = 7, Name = "raspberries", Price = 2.20 });

In your view you can create a drop down list like so:

@model ProductModel

@Html.DropDownListFor(n => n.MyProductId, 
                         new SelectList(product, "ProductId", "Name"))

For more info you can have a look at this article

etrupja
  • 2,710
  • 6
  • 22
  • 37
0

Give this a go :)

@model App.Models.Model

@Html.DropDownListFor(x => x.ProductId, new SelectList(Product, "Name", "Price"))
David
  • 641
  • 1
  • 8
  • 23