使用ajax将对象发送到mvc控制器

sha*_*ikh 2 c# ajax

我在一个对象中传递多个参数,然后将其传递给控制器​​中的方法。它正在调用该方法,但它没有携带从 ajax 调用发送到该方法的数据。当我要检查模型的对象时,它显示为空。那么我可以以这种方式发送数据还是应该尝试其他方法?预先感谢请帮助我。这是我的代码。

var Color = [], Material = [], Size = [], FinishingType = [], Style = [];
        $('.productFilterLabelList .filterList').on('change', '[type=checkbox]', function () {
            debugger;
           
            var Main = {};
            var filterType = $(this).parents('.productFilterLabelList').find('.hdn-filter-type').val();
            var filterTypeID = $(this).val();
            var ischeked = $(this).is(':checked');
            if (ischeked) {
                if (filterType == 'color') {
                    Color.push(filterTypeID);
                }
                else if (filterType == 'size') {
                    Size.push(filterTypeID);
                }
                else if (filterType == 'finsih') {
                    FinishingType.push(filterTypeID);
                }
                else if (filterType == 'material') {
                    Material.push(filterTypeID)
                }
                else {
                    Style.push(filterTypeID);
                }
            }
            else {
                alert('hello');
                if (filterType == 'color') {
                    Color.pop(filterTypeID);
                }
                else if (filterType == 'size') {
                    Size.pop(filterTypeID);
                }
                else if (filterType == 'finsih') {
                    FinishingType.pop(filterTypeID);
                }
                else if (filterType == 'material') {
                    Material.pop(filterTypeID)
                }
                else {
                    Style.pop(filterTypeID);
                }
            }
            Main = {
                Color: Color,
                Size: Size,
                FinishingType: FinishingType,
                Material: Material,
                Style: Style
            }
            console.log(Main);
            $.ajax({
                url: '/Home/SearchByAllFilterTags',
                type: "Get",
                contentType: "application/json",
                dataType: "json",
                data: '{Main:' +JSON.stringify(Main)+' }',
                success: function (results) {
                   
                }
            })
        });
        
 public ActionResult SearchByAllFilterTags(ProductFilterViewModel Main)
    {
        return Json("", JsonRequestBehavior.AllowGet);
    }`public class ProductFilterViewModel
{
    public int[] Color { get; set; }
    public int[] Material { get; set; }
    public int[] Size { get; set; }
    public int[] FinishingType { get; set; }
    public int[] Style { get; set; }
    public int[] Pattern { get; set; }
    //public string FilterText { get; set; }
    //public List<ProductFilterViewModel> FilterTextList { get; set; }
}`

      
Run Code Online (Sandbox Code Playgroud)

Alb*_*rtK 5

您不需要对对象进行字符串化。只需传递你的Main对象:

$.ajax({
    url: '/Home/SearchByAllFilterTags',
    type: "Get",
    contentType: "application/json",
    dataType: "json",
    traditional: true,
    data: Main,
    success: function (results) {
    }
})
Run Code Online (Sandbox Code Playgroud)

编辑:

如果操作方法中的数组为空,请尝试添加traditional: true到ajax设置中