-1

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>

Html View

anand
  • 1,559
  • 5
  • 21
  • 45
  • 1
    Your dropdownlist has the name `name="ProductList"`, but your method parameter name is `ProductCode` - they need to match - change one or the other. Then learn to start using view models and strongly bind to your model using the `XXXFor()` HtmlHelper methods –  Feb 11 '16 at 06:15
  • check this :http://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc4/27901225#27901225 – Ehsan Sajjad Feb 11 '16 at 10:03

4 Answers4

0

First there is error in your code, your giving selected value to store in 'ProductList' but in the controller of post GetList you are giving variable ProductCode.
You can easily understand the process by following the below steps
1. DataBase

Table - tblProducts
    id   ProductCode    ProductName 
    1    name_x         name_x
    2    code_y         name_y

2. In controller side
a. If we are not fetching data from db and then in the [HttpGet] method do like the following

        private ApplicationDbContext db = new ApplicationDbContext();
            [HttpGet]
            public ActionResult GetList()
            {
                ViewBag.ProductList = new SelectList(db.tblProducts, "ProductCode", "ProductName");
                return View();

            }

3. In the view
import model in the view page at the top of page @model ProjectName.Models.ModelName @Html.DropDownList("ProductList", "Select Product")
4. Then in the controller, to receive the selected data

[HttpPost]
public ActionResult GetList(string ProductList)
{
    //your selected value will stored in the ProductList variable
    return View();
}

5. edit the model file as below

using System; 
using System.Collections.Generic;
namespace ProjectName.Models
{
     public class tblProduct 
     { 
         public string ProductCode { get; set; } 
         public string ProductName { get; set; } 
         public string SBU { get; set; } 
     } 
 }

hope this helps

anand
  • 1,559
  • 5
  • 21
  • 45
-1

You can Highlight the value which you want to be select

[HttpPost]
public ActionResult GetList(int? ProductList)
{
    ...
} 
Imran Luhur
  • 447
  • 3
  • 15
  • sorry for miss understanding you can use 'GetList(int? ProductList)' into your post method to get the selected value form dropdownlist. – Imran Luhur Feb 11 '16 at 06:34
  • 'int?' is used if your productcode is integer other then you may use string also instead of int?, – Imran Luhur Feb 11 '16 at 06:39
-1

// ActionMethod

public ActionResult GetList()
    {
        using (mvcEntities objEntity = new mvcEntities())
        {
            List<tblProduct> Products = objEntity.tblProduct.ToList();
            ViewBag.ProductList = new SelectList(Products, "ProductCode", "ProductName");
            return View();
        }
    }

// In View

@using (@Html.BeginForm("GetList", "Home", FormMethod.Get))
{
if (ViewBag.Pro != null)
{
@Html.DropDownList("Pro", "Select Product")}
}
 <br />
 <input type="submit" name="btnSub" id="btnSub" value="Submit" /> 
 }

 @{
 mvcEntity objEntity = new mvcEntity();
 int ProCode = 0;
 if (Request["Pro"] != null)
 {
    ProCode = int.Parse(Request["Pro"]);
    tblProduct pmd = objEntity.tblProduct.Where(x => x.ProductCode == ProCode).FirstOrDefault();
<table border="1">
    <thead>
        <tr>
            <th>Product ID</th>
            <th>Product Code</th>
            <th>Product Name</th>

        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@pmd.ProductId</td>
            <td>@pmd.ProductCode</td>
            <td>@pmd.ProductName</td>
        </tr>
    </tbody>
</table>
}   

}

-2

ypu should use Viewbag.ProductList instead of direct calling.

@using (@Html.BeginForm("GetList", "Home", FormMethod.Post, new { id = "ProductLi" }))
{  
@Html.DropDownList("ViewBag.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>
Priya
  • 1,359
  • 6
  • 21
  • 41