Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AJAX将多个数据发布到ASP.NETMVC_C#_Jquery_Ajax_Asp.net Mvc 4 - Fatal编程技术网

C# AJAX将多个数据发布到ASP.NETMVC

C# AJAX将多个数据发布到ASP.NETMVC,c#,jquery,ajax,asp.net-mvc-4,C#,Jquery,Ajax,Asp.net Mvc 4,在通过ajax jquery将多个对象发布到MVC4控制器时,我遇到了一个问题。已经好几个星期了,但我似乎找不到解决办法。 我尝试了几种方法,有时filterModel对象为null,有时字符串参数为null(无论是否指定contentType,即使我将字符串化也无所谓) 我想要什么? 我想传递三个对象:1。filterModel 2.testparamA 3.testparamB 我应该如何将这三个对象传递给MVC控制器?我需要在数据中写入什么:所以我得到所有3个对象值 最简单的控制器 [Ht

在通过ajax jquery将多个对象发布到MVC4控制器时,我遇到了一个问题。已经好几个星期了,但我似乎找不到解决办法。 我尝试了几种方法,有时filterModel对象为null,有时字符串参数为null(无论是否指定contentType,即使我将字符串化也无所谓)

我想要什么? 我想传递三个对象:1。filterModel 2.testparamA 3.testparamB 我应该如何将这三个对象传递给MVC控制器?我需要在数据中写入什么:所以我得到所有3个对象值

最简单的控制器

[HttpPost]
public JsonResult Test(string testparamA, string testparamB, FilterModel filter)
{
    using (RBSystemEntities repository = new RBSystemEntities())
    {
        return Json(new {
            DataList = repository.Items.Select(x => new {x.PKID, x.ItemName}).ToList(),
            Result = "OK"
        });
    }
}
最简单的观点

var filterModel = @Html.Raw(Json.Encode(new FilterModel("ItemName", "Pepperoni Pizza")))
//filterModel = JSON.stringify(filterModel);

function testme() {
    // post the javascript variable back to the controller 
    $.ajax({
        url: '/Menu/Test',
        type: 'POST',
        //contentType: 'application/json; charset=utf-8',
        data: {
            filter: filterModel,
            testparamA: 'A value',
            testparamB: 'B value'
        }, // with this approach I get filterModel null in the controller however testparamA and testparamB has values
        data: filterModel, // with this approach I get values for filterModel but I can't pass testparamA and testparamB
        success: function (result) {
            // TODO: do something with the results
            alert('success');
        }
    });
}
testme();
最简单的FilterModel类

public class FilterModel
{
    public FilterModel() { }
    public FilterModel(string filtercolumn, string filtervalue)
    {
        this.FilterColumn = filtercolumn;
        this.FilterValue = filtervalue;
        this.FilterColumnCriteria = "=";
    }
    public string FilterColumn { get; set; }
    public string FilterValue { get; set; }
    public string FilterColumnCriteria { get; set; }
}

@PaulZahra引导我走向正确的方向

以下更改修复了我的问题:


希望你不介意我发布我的评论(这很有帮助)作为回答

如果您按如下方式使用stringify,它应该可以工作

JSON.stringify({ fm: filterModel, ta: testparamA, tb: testparamA })

您是否为
FilterModel
编写了ModelBinder?您是说data:JSON.stringify({fm:FilterModel,ta:testparamA,tb:testparamA})不起作用?读一读@PhilippM,我没有写一个ModelBinder。它应该由MVC自动绑定,因为data:filterModel可以完美地工作并将数据发送给MVC。但是数据:{filter:filterModel,testparamA:'A value',testparamB:'B value'}不起作用如果你有一个ModelBinder,MVC将区分平面参数和
filterModel
的对象,我想,但是我现在不能测试它。@PaulZahra谢谢。现在我意识到,我应该对整个过程进行字符串化,而不仅仅是filterModel:)很高兴它能工作:)使用{datatype:'json'…}的$.ajax post可以很好地工作
JSON.stringify({ fm: filterModel, ta: testparamA, tb: testparamA })