问题
There are 2 dropdowns. One is Category and another subcategory. Once i select a category in the drop down, i am firing a JS where it brings all the subcategories for the selected category. I got the values but when trying to bind the array data, i am getting the error
Object doesn't support property or method 'forEach'
Model
public class NewRequestView
{
public string SelectedCategory { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
public List<SelectListItem> SubCategory { get; set; }
}
Controller
public NewRequestView SubCategoriesLookup([FromBody] NewRequestView model1)
{
var newreuest = new NewRequestView();
var lookups = GetSubCategories(model1.SelectedCategory);
newreuest.SubCategory= iMapper.Map<List<SubCategory>, List<SelectListItem>>(lookups);
return newreuest;
}
My Java script looks like this
$(document).ready(function () {
$('#Category').change(function () {
debugger;
var selectedCategory = GetSelectedItemFromDropDown('#Category');
if (selectedCategory!= undefined && selectedCategory.length == 1) {
var req = { SelectedCategory: selectedCategory[0] };
POST('Home/SubCategoriesLookup', JSON.stringify(req), categoryLookupReceived, errorPostback, undefined);
};
});
});
The success of ajax method calls the binding of dropdown
categoryLookupReceived= function (data, additionalData) {
debugger;
bindMultiSelectDropdown($("#SubCategoryDDL"), data);}
And the bindMultiselectDropdown function look likes below in which i get the error when using the foreach.
function bindMultiSelectDropdown(dropdown, arrDataSource, firstItem) {
var dataSource = [];
if (firstItem != undefined) dataSource.push(firstItem);
if (arrDataSource != undefined) arrDataSource.forEach(function (s) { dataSource.push({ label: s.Text, value: s.Value }); });
$(dropdown).multiselect('dataprovider', dataSource);}
Let me know what is the mistake i am doing.
来源:https://stackoverflow.com/questions/60799823/0x800a01b6-javascript-runtime-error-object-doesnt-support-property-or-method