0
code For It

I made Two Dropdownlists One for the Category and the second Product When i choose a a itm form dropdown category then the Products dropdown list should Show the products according to category that i select from category dropdown enter image description here

  public partial class Product
        {
            public int ProductID { get; set; }
            public int CategoryID { get; set; }
            public string ProductName { get; set; }
        }
    }
    public partial class Category
        {
            public int CategoryID { get; set; }
            public string CategoryName { get; set; }
            public virtual ICollection<Product> Products { get; set; }
        }

     public ActionResult Create()
            {
                ViewBag.vbCategoryList = new SelectList(db.Categories.Select(x => new { Value = x.CategoryID, Text = x.CategoryName}), "Value", "Text");
                ViewBag.vbProductList = new SelectList(db.Products.Select(x => new { Value = x.ProductID, Text = x.ProductName }), "Value", "Text");
                return View();
            }

    <table class="table">
                <tr>
                    <td>Category</td>
                    <td>Product</td>
                    <td>Quantity</td>
                    <td>Rate</td>
                    <td>&nbsp;</td>
                </tr>
                <div id="Tbody1">
                <tr class="mycontainer" id="mainrow">
                    <td>
                        @Html.DropDownListFor(model => model.CategoryID, (SelectList)ViewBag.vbCategoryList, "Select One", htmlAttributes: new { @class = "form-control" })
                    </td>
                    <td>`
                        @Html.DropDownListFor(model => model.ProductID, (SelectList)ViewBag.vbProductList, "Select One", htmlAttributes: new { @class = "form-control" })
                    </td>
                    <td></td>
                    <td>
                        <input type="text" id="rate" class="rate form-control" />
                    </td>
                    <td>
                        <input type="button" id="AddMore" style="width:80px" value="AddMore" class="btn btn-success  /">
                    </td>
                </tr>
                </div>

[enter image description here][2]

Shahab
  • 11
  • 6
  • https://stackoverflow.com/questions/12258839/populate-dropdown-list-based-on-another-dropdown-list ..Must have look at this – Dhawal Jun 14 '17 at 09:43

1 Answers1

0

Do one thing..When on page Load fill the category DropDown using stored procedure in this

            if (!IsPostBack)//in pageload
            {               
              objENT = new ENT();
                objENT.ProcType = "ZONEMASTER";
                DataSet dsLoad = BLL.CdInput(objENT);
                FillCombo(dsLoad.Tables[0], ddlZone);             
            }
void FillCombo(DataTable dt, DropDownList ddl)//outside pageload
        {
            ddl.DataSource = dt;
            ddl.DataTextField = "Name";
            ddl.DataValueField = "ID";
            ddl.DataBind();

        }

then in

protected void ddlZone_SelectedIndexChanged(object sender, EventArgs e)
    {
      objENT.ProcType = "procedure";
        objENT.strZone = ddlZone.SelectedItem.Value;
        DataSet dsLoad = BLL.CdInput(objENT);
        FillCombo(dsLoad.Tables[0], ddlStore);

    }

get the value of the Dropdown and then send the Dropdown.SelectedItem.Value to database and get the Product accordingly this is the concept I used u can change it as u wish give it a try :)

Frost_Mourne
  • 342
  • 4
  • 22