Pagliu
❔ I would like to use an SQL query on a consecutive Select HTML element.
ASP .NET, by the way I solved!
Executed this JS script on my GET rest API
GET API
@section Scripts{
<script>
var categorySelect = document.getElementById("categorySelect");
var subCategorySelect = document.getElementById("subCategorySelect");
categorySelect.addEventListener("change", function () {
var selectedCategoryValue = categorySelect.value;
$.ajax({
url: '/Admin/Product/GetSubCategoriesBasedOnCategory?id=' + selectedCategoryValue,
method: 'GET',
success: function (data) {
subCategorySelect.innerHTML = '';
if (data.length > 0) {
subCategorySelect.disabled = false;
} else {
subCategorySelect.disabled = true;
}
data.forEach(function (item) {
var option = document.createElement("option");
option.text = item.text;
option.value = item.value;
subCategorySelect.appendChild(option);
});
}
});
});
</script>
@{
<partial name="_ValidationScriptsPartial" />
}
}
@section Scripts{
<script>
var categorySelect = document.getElementById("categorySelect");
var subCategorySelect = document.getElementById("subCategorySelect");
categorySelect.addEventListener("change", function () {
var selectedCategoryValue = categorySelect.value;
$.ajax({
url: '/Admin/Product/GetSubCategoriesBasedOnCategory?id=' + selectedCategoryValue,
method: 'GET',
success: function (data) {
subCategorySelect.innerHTML = '';
if (data.length > 0) {
subCategorySelect.disabled = false;
} else {
subCategorySelect.disabled = true;
}
data.forEach(function (item) {
var option = document.createElement("option");
option.text = item.text;
option.value = item.value;
subCategorySelect.appendChild(option);
});
}
});
});
</script>
@{
<partial name="_ValidationScriptsPartial" />
}
}
[HttpGet]
public IActionResult GetSubCategoriesBasedOnCategory(int id)
{
IEnumerable<SubCategory> all = _unitOfWork.SubCategory.GetAll();
IEnumerable<SubCategory> subCategories = _unitOfWork.SubCategory.GetList(u => u.CategoryId == id);
IEnumerable<SelectListItem> listItems = subCategories.Select(subCategory => new SelectListItem
{
Text = subCategory.Name,
Value = subCategory.Id.ToString()
});
return new JsonResult(listItems);
}
[HttpGet]
public IActionResult GetSubCategoriesBasedOnCategory(int id)
{
IEnumerable<SubCategory> all = _unitOfWork.SubCategory.GetAll();
IEnumerable<SubCategory> subCategories = _unitOfWork.SubCategory.GetList(u => u.CategoryId == id);
IEnumerable<SelectListItem> listItems = subCategories.Select(subCategory => new SelectListItem
{
Text = subCategory.Name,
Value = subCategory.Id.ToString()
});
return new JsonResult(listItems);
}
8 replies