【问题标题】:Send object to mvc controller using ajax使用 ajax 将对象发送到 mvc 控制器
【发布时间】:2018-02-14 16:12:44
【问题描述】:

我在一个对象中传递多个参数,然后将它传递给控制器​​中的一个方法。它击中了方法,但没有携带要从 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; }
}`

      

【问题讨论】:

  • 您是否尝试过简单地传递 Main 本身?
  • 是的,我在它击中控制器方法之前已经尝试过了,但无法在模型对象中找到数据到方法中。
  • 只需删除 json.stringify 所以你的代码必须是这样的 { 'Main': Main},

标签: c# ajax


【解决方案1】:

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

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

编辑:

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

【讨论】:

  • 只传递了主对象,但仍然没有在控制器上获取数据,它仍然显示为空。
【解决方案2】:

您不需要进行字符串化。使用这种格式:

 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,
            success: function (results) {

            }
        })
    });

只要你的json数据中没有加引号,你的数据就不会通过。

如果您在控制器上的模型匹配,这将起作用。

【讨论】:

  • 模型相同,但仍然没有在模型中获取数据。
  • 从 ajax 请求中移除内容类型
  • 是的,它击中了模型中的方法和 1 个数组,但它不是我推入变量的实际值,它在数组中显示 0
  • 我认为它显示数组索引 no 而不是 value
  • 好的,谢谢,但是 ajax 已经在使用方法了。数据还是有问题 非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2021-11-07
  • 2014-02-28
  • 2020-04-17
  • 2013-09-26
  • 1970-01-01
相关资源
最近更新 更多