Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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
JQUERY ajax将JSON发布到C#MVC控制器,但传入的数据为空_C#_Jquery_Json_Ajax_Asp.net Mvc - Fatal编程技术网

JQUERY ajax将JSON发布到C#MVC控制器,但传入的数据为空

JQUERY ajax将JSON发布到C#MVC控制器,但传入的数据为空,c#,jquery,json,ajax,asp.net-mvc,C#,Jquery,Json,Ajax,Asp.net Mvc,我有一个奇怪的问题。我有一个C#数据结构,有两个类: public class AddQuestionModel2 { public int? QuestionID { get; set; } public string QuestionString { get; set; } public int? TrueAnswer { get; set; } public QuestionType Type { get; set; } public IEnumer

我有一个奇怪的问题。我有一个C#数据结构,有两个类:

public class AddQuestionModel2
{
    public int? QuestionID { get; set; }
    public string QuestionString { get; set; }
    public int? TrueAnswer { get; set; }
    public QuestionType Type { get; set; }
    public IEnumerable<AddQuestionModelAnswer> Answers { get; set; }
}

public class AddQuestionModelAnswer
{
    public int? ID { get; set; }
    public string AnswerString { get; set; }
    public bool? IsRight { get; set; }
    public int? Order { get; set; }
}

public enum QuestionType
{
    SingleSelect,
    MultiSelect,
    OrderAnswers,
    FillingGap,
    ExampleRequired,
    TrueOrFalse
}
在C#端,我有以下方法来接收post数据:

[HttpPost]
    public async Task<string> Add([FromBody] AddQuestionModel2 q)
    {
        var ctx = this.HttpContext;
        JsonResultModel res = new JsonResultModel();
    }
[HttpPost]
公共异步任务添加([FromBody]AddQuestionModel2 q)
{
var ctx=this.HttpContext;
JsonResultModel res=新的JsonResultModel();
}
参数“q”始终为空。如果展开ctx(HttpContext),则会抛出Request.Form数据System.InvalidOperationException

如果你知道可能出了什么问题,你有什么想法吗? 最大的问题是我无法调试HttpContext中正在发生的事情以及它为什么抛出异常

提前谢谢!
Gabor

在ajax代码中尝试data:data,而不是data:JSON.stringify(data)。stringify()方法将JavaScript对象或值转换为JSON字符串,但ajax数据需要JavaScript对象。在布尔字段中使用“真”或“假”来代替0或1的另一种尝试


在《邮递员》中,一切正常。

我添加了问题类型的定义。它是一个枚举,所以在客户端,它作为数字进行管理。IsRight是类中的bool,在客户端它是1或0。我尝试使用data:data。同样的结果。也尝试了真/假。同样的结果,你是对的。C#无法将0和1转换为false和true。我必须使用纯javascript true和false。因为字符串“true”和“false”也不被接受。我还必须将Type的值从string转换为integer,因为C#无法自动将“0”转换为0。
$.ajax({
                url: "/Questions/Add",
                method: "POST",
                async: true,
                dataType: "json",
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                success: function (e) {
                    if (e.Success) {
                        document.location = "/Questions";
                    }
                },
                error: function (e) {
                    var i;
                    for (i in e) {
                        console.log(e[i]);
                    }
                }
            });
[HttpPost]
    public async Task<string> Add([FromBody] AddQuestionModel2 q)
    {
        var ctx = this.HttpContext;
        JsonResultModel res = new JsonResultModel();
    }