简体   繁体   English

将JSON对象发布到ASP MVC无法正常工作

[英]Post json object to ASP MVC doesn't work properly

I'm trying to post a custom Json object to ASP.NET MVC controller, but doesn't work properly. 我正在尝试将自定义Json对象发布到ASP.NET MVC控制器,但无法正常工作。

My JS Code is: 我的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');
        }
    });
});

In my Controller: 在我的控制器中:

[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,
}

When get the HttpPost the properties FirstItem and SecondItem always been null. 当获取HttpPost时,属性FirstItem和SecondItem始终为null。 发布JsonObject

Thanks in advance. 提前致谢。

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');
        }
    });
});

in your code you are not specifying the content type.Just try this 在您的代码中,您没有指定内容类型。

Not a proper solution but try: 不是适当的解决方案,但请尝试:

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

It's because of how jQuery translates your JSON object into POST parameters messes up C#'s ability to read it (jQuery tries to map the properties of the javascript object to variables on the post, MVC is smart enough though to just have raw JSON posted a it). 这是因为jQuery如何将JSON对象转换为POST参数,从而破坏了C#的读取能力(jQuery尝试将javascript对象的属性映射到帖子上的变量,MVC足够聪明,尽管只是将原始JSON发布到了它) 。

The simplest solution is just to stringify it first and it should deserialize properly ( data: JSON.stringify(myData) ). 最简单的解决方案是首先对其进行字符串化,并且应该正确地反序列化( data: JSON.stringify(myData) )。

Note that if you're supporting old browsers you'll need to add the json2.js library for that function, but it's built in in all modern browsers. 请注意,如果您支持旧的浏览器,则需要为该功能添加json2.js库,但该库已内置在所有现代浏览器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM