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> </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]