E-mail
http://www.mikesdotnetting.com/Article/128/Get-The-Drop-On-ASP.NET-MVC-DropDownLists
1) SelectListItem – Value and Text property assigned
var db = new northwindDataContext();IEnumerable<SelectListItem> items = db.Categories.Select(c => new SelectListItem {Value = c.CategoryID.ToString(), Text = c.CategoryName});ViewData["CategoryID"] = items;
<%= Html.DropDownList("CategoryID") %>
<%= Html.DropDownList("CategoryID", (IEnumerable<SelectListItem>)ViewData["Categories"]) %>
var query = db.Categories.Select(c => new {c.CategoryID , c.CategoryName});ViewData["Categories"] = new SelectList(query.AsEnumerable(), "CategoryID", "CategoryName",3);
On the product view:
<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "TheForm" })) {%> <%=Html.DropDownList("CategoryID", (SelectList)ViewData["Categories"], new { onchange="this.form.submit();"})%><%}%>
public ActionResult Index(int? categoryid){ var db = new northwindDataContext(); var query = db.Categories.Select(c => new {c.CategoryID, c.CategoryName}); ViewData["Categories"] = new SelectList(query.AsEnumerable(), "CategoryID", "CategoryName"); List<Product> products; if (categoryid == null) products = db.Products.ToList(); else products = (from p in db.Products where p.CategoryID == categoryid select p).ToList(); return View(products);}
Remember Me
a@href@title, strike