Here my approach is database first approach in MVC which have entity framework name "mvcEntity" and tblProducts is Products stored in my Database where DbContext has been disposed. I used ProductCode as my ValueField and ProductName as TextField for DropDownList.
//In Controller:
public ActionResult GetList()
{
using (mvcEntities objEntity = new mvcEntities())
{
List<tblProduct> Products = objEntity.tblProducts.ToList();
ViewBag.ProductList = new SelectList(Products, "ProductCode", "ProductName");
return View(Products);
}
}
[HttpPost]
public ActionResult GetList(string ProductCode)
{
//Get the Selected Dropdown Value an Dispaly in a table
return View();
}
// In View.cshtml:
@using (@Html.BeginForm("GetList", "Home", FormMethod.Post, new { id = "ProductLi" }))
{
@Html.DropDownList("ProductList", "Select Product")`}
<br />
<table border="1">
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th>Remarks</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>