0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'forEach'

我的未来我决定 提交于 2020-05-17 03:29:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!