So basically I'm trying to make a website that can do stuff like the programs in the super markets database with products / Views where sellers work , Views to add new products and receipt note model.So I have a class
public class Product
{
public int ID { get; set; }
public string Title { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}
public class ProduktiDBContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
and a controller
public class ProduktiController : Controller
{
private ProduktiDBContext db = new ProduktiDBContext();
public ActionResult Index()
{
return View(db.Products.ToList());
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
}
and in the Index View I want to add a DropDownList with all the titles of the products from the database
@model IEnumerable<Produkti.Models.Product>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
<hr />
@foreach (var item in Model) {
<tr>
<td>
//I'd like to put the dropdownlist here i dont know if this for loop is necessary
</td>
<td>
</td>
</tr>
}
</table>
I was wondering if I need to pass a List with all the titles from the controller to the View or the return View(db.Products.ToList()); from the Index Method already pass the data that I need, and how to pass data from this DropDownList