Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This Question in addition, I guess, Got everything you are looking for:</p> <p><a href="https://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor">How to write a simple Html.DropDownListFor()?</a></p> <p>As a beginner, I did a very basic implementation of dropDownlist using the NorthWind Database only.</p> <p>I had imported the Product &amp; Suppliers table from Northwind database.</p> <p>In the <code>ProductController.cs</code> file, which is the controller file for my <code>Product</code> table, add method: <code>GetAllSuppliers</code> to get all SuppliersID which we will display in a dropdown.</p> <pre><code>public IEnumerable&lt;int&gt; GetAllSuppliers() { NorthwindEntities db = new NorthwindEntities(); return db.Suppliers.Select(e =&gt; e.SupplierID); } </code></pre> <p>Now, in the <code>Create</code> action method in <code>ProductController.cs</code>, pass all the values of <code>SupplierID</code> in <code>ViewData</code> as seen below:</p> <pre><code> public ActionResult Create() { ViewData["Suppliers"] = new SelectList(GetAllSuppliers()); return View(new Product()); } </code></pre> <p>In your corresponding <code>Create.aspx View</code>, use this:</p> <pre><code>&lt;%: Html.DropDownListFor(model =&gt; model.SupplierID, ViewData["Suppliers"] as SelectList) %&gt; </code></pre> <p>Below is a snapshot of the Result:</p> <p><img src="https://i.stack.imgur.com/FxFLn.png" alt="enter image description here"></p> <p>Let me know if you need any explanation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload