i want to fill dropdownlist by another dropdownlist. this is category and sub category.
first dropdownlist is main category fields in database: 1.Id 2.CatName
Second dropdownlist is subCategory fields in database: 1.Id 2.MainId 3.catName
well it is true, now: my razor codes:
@functions {
public List<MainProduceCategory>
CatList(int mainCatId)
{
ApplicationDbContext _context = new ApplicationDbContext();
IMainProduceRepository cats = new MainProduceRepository(_context);
return cats.GetCats(items);
}
}
the code is true and return subcategories by maincatId.
<div class="form-group">
<label class="control-label"></label>
<select class="custom-select simplebox form-control" onChange="myjs(this.value);">
<option value="1">movies</option>
<option value="2">softwares</option>
</select>
</div>
the code is Main Categories in razor.
<div class="form-group">
<label class="control-label"></label>
<select id="subcats" class="custom-select simplebox form-control">
<option value="0">not select</option>
</select>
</div>
the code is sub Categories in razor.
now: how to fill Second dropdownlist by main dropdownlist?
jquery code:but not work.
<script language="javascript">
function myjs(state) {
with (document.getElementById('subcats')) {
options.length = 0;
if (state == 0) {
options[0] = new Option('mainSelect', '0');
}
if (state == "1") {
for (var i = 0; i <= @CatList(1).Count; i++) {
options[i] = new Option(@CatList(1).Select(c=>c.CatName)[i]);
}
}
}
}
</script>