Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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# 将json对象发布到ASP MVC不';我不能正常工作_C#_Ajax_Json_Asp.net Mvc 4_Json.net - Fatal编程技术网

C# 将json对象发布到ASP MVC不';我不能正常工作

C# 将json对象发布到ASP MVC不';我不能正常工作,c#,ajax,json,asp.net-mvc-4,json.net,C#,Ajax,Json,Asp.net Mvc 4,Json.net,我试图将自定义Json对象发布到ASP.NET MVC控制器,但无法正常工作 我的JS代码是: var myData = { "Message": "Another message", "Value": 4, "FirstItem": { "ItemName": "Este es el primer Item" }, "SecondItem": { "ItemName": "Este es el segundo Item"

我试图将自定义Json对象发布到ASP.NET MVC控制器,但无法正常工作

我的JS代码是:

var myData = {
    "Message": "Another message",
    "Value": 4,
    "FirstItem": {
        "ItemName": "Este es el primer Item"
    },
    "SecondItem": {
        "ItemName": "Este es el segundo Item"
    }
};

$('.clickeable').click(function () {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: myData,
        url: '@Url.Action("PostSomething")',
        success: function () {
            console.info('Success');
        },
        error: function () {
            console.info('Error');
        }
    });
});
在我的控制器中:

[HttpPost]
public JsonResult PostSomething(SimpleModel model)
{
    return Json(true);
}

public class SimpleModel
{
    public string Message { get; set; }
    public int Value { get; set; }
    public SimpleItem FirstItem { get; set; }
    public SimpleItem SecondItem { get; set; }
}

public class SimpleItem
{
    public string ItemName { get; set; }
    //more depth,
}
获取HttpPost时,属性FirstItem和SecondItem始终为null。


提前感谢。

这不是一个合适的解决方案,但请尝试:

[HttpPost]
public JsonResult PostSomething(string Message, int Value,SimpleItem FirstItem,SimpleItem  SecondItem )
{
    return Json(true);
}

这不是一个合适的解决方案,但请尝试:

[HttpPost]
public JsonResult PostSomething(string Message, int Value,SimpleItem FirstItem,SimpleItem  SecondItem )
{
    return Json(true);
}

这是因为jQuery是如何将JSON对象转换为POST参数的,这打乱了C#的读取能力(jQuery试图将javascript对象的属性映射到POST上的变量,尽管MVC非常聪明,只需将原始JSON发布到它)

最简单的解决方案就是首先对其进行字符串化,并且它应该正确地进行反序列化(
data:JSON.stringify(myData)


请注意,如果您支持旧浏览器,则需要为该功能添加json2.js库,但它是所有现代浏览器中内置的。

这是因为jQuery如何将JSON对象转换为POST参数,会使C#的读取能力受到影响(jQuery尝试将javascript对象的属性映射到post上的变量,尽管MVC非常聪明,只需将原始JSON发布到它)

I have tried your scenario.I found out solution that

var myData = {
    "Message": "Another message",
    "Value": 4,
    "FirstItem": {
        "ItemName": "Este es el primer Item"
    },
    "SecondItem": {
        "ItemName": "Este es el segundo Item"
    }
};

    var obj = { 'model': myData };
    var val=JSON.stringify(obj);

$('.clickeable').click(function () {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: val,
        contentType:"application/json",
        url: '@Url.Action("PostSomething")',
        success: function () {
            console.info('Success');
        },
        error: function () {
            console.info('Error');
        }
    });
});
最简单的解决方案就是首先对其进行字符串化,并且它应该正确地进行反序列化(
data:JSON.stringify(myData)

请注意,如果您支持旧浏览器,则需要为该函数添加json2.js库,但它内置于所有现代浏览器中

I have tried your scenario.I found out solution that

var myData = {
    "Message": "Another message",
    "Value": 4,
    "FirstItem": {
        "ItemName": "Este es el primer Item"
    },
    "SecondItem": {
        "ItemName": "Este es el segundo Item"
    }
};

    var obj = { 'model': myData };
    var val=JSON.stringify(obj);

$('.clickeable').click(function () {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: val,
        contentType:"application/json",
        url: '@Url.Action("PostSomething")',
        success: function () {
            console.info('Success');
        },
        error: function () {
            console.info('Error');
        }
    });
});
在代码中没有指定内容类型。请尝试此操作

在代码中没有指定内容类型。请尝试此操作