Hello All,
I have staredd newly learning mvc-5 framwork.
I have 2 dropdowns in my view. I want to populate sub-category list based on category selection.
This is my code.
<div class="form-group">
@Html.LabelFor(model => model.category_id, "category_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("category_id",null,"Please Select", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.sub_category_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sub_category_id, "sub_category_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("sub_category_id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.sub_category_id, "", new { @class = "text-danger" })
</div>
</div>
// GET: Admin/NewAsset
public ActionResult Index()
{
ViewBag.category_id = new SelectList(objBs.CategoryBs.GetAll().ToList(), "s_no", "main_category");
ViewBag.sub_category_id=new SelectList(objBs.SubCategoryBs.GetAll().ToList(),"s_no","sub_category");
return View();
}
Please let me know the best way to achieve this functionality.