将mson发送到控制器时,ASP.NET mvc 4控制器参数始终为null,为什么?

Gaš*_*dič 15 asp.net-mvc json controller object

这里已经有一些类似的帖子,并尝试了所有解决方案,但仍然无效......我无法在控制器中获取值,它始终为null.下面是代码.我错过了什么吗?

客户端javascript

   function getChart() {
       JSONString3 = { HAxis : [{ Name : "monday" }] };
       jQuery.ajaxSettings.traditional = true;
        $.ajax({
            url: "@Url.Action("getChart","SBM")",
            type: 'POST',
            contentType: 'json',
            dataType: 'html',
            data: JSONString3,
            success: function (data) {
                var imagestring = btoa(data);
                $('#ChartImage').attr('src', "data:image/png;base64," + imagestring + "?" + new       Date().getTime());
            }
        })
        jQuery.ajaxSettings.traditional = false;
    }
Run Code Online (Sandbox Code Playgroud)

MVC控制器

    [Authorize]
    [HttpPost]
    public ActionResult getChart(YAxis HAxis)
    {
        YAxis XAxisvalue = HAxis;
        Charts chart = new Charts();
        MemoryStream ms = new MemoryStream();
        chart.Chart.SaveImage(ms);
        string image = Convert.ToBase64String(ms.GetBuffer());
        return File(ms.GetBuffer(), "image/png", "Chart.png");
    }
Run Code Online (Sandbox Code Playgroud)

模型

public class YAxis
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Gaš*_*dič 23

谢谢你们的指示和解决方案.解决方案是您所有建议的组合,因此我决定将其整理到一个帖子中.

解决问题的方法如下:

  1. contentType应该是application/json(如上面提到的Ant P)
  2. json数据应该是JSONString3 = {"Name" : "monday" }(如上面建议的Ant P)
  3. 在发送到控制器之前,json应该stringifyed如下:( JSONString3 = JSON.stringify(JSONString3)如Quan建议的那样)

客户端javascript

function getChart() {
               JSONString3 = { "Name" : "monday" };
               jQuery.ajaxSettings.traditional = true;
                $.ajax({
                    url: "@Url.Action("getChart","SBM")",
                    type: 'POST',
                    contentType: 'application/json',
                    dataType: 'html',
                    data: JSON.stringify(JSONString3),
                    success: function (data) {
                        var imagestring = btoa(data);
                        $('#ChartImage').attr('src', "data:image/png;base64," + imagestring + "?" + new       Date().getTime());
                    }
                })
                jQuery.ajaxSettings.traditional = false;
    }
Run Code Online (Sandbox Code Playgroud)

MVC控制器

[Authorize]
[HttpPost]
public ActionResult getChart(YAxis HAxis)
{
    YAxis XAxisvalue = HAxis;
    Charts chart = new Charts();
    MemoryStream ms = new MemoryStream();
    chart.Chart.SaveImage(ms);
    string image = Convert.ToBase64String(ms.GetBuffer());
    return File(ms.GetBuffer(), "image/png", "Chart.png");
}
Run Code Online (Sandbox Code Playgroud)

模型

public class YAxis
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

JSONString3 = { "Name" : "monday" };
Run Code Online (Sandbox Code Playgroud)

我们做得到:

var JSONString3 = {};
JSONString.Name = "monday";
Run Code Online (Sandbox Code Playgroud)

但我们仍然需要在发布到控制器之前对对象进行字符串化!

要将多个对象传递给控制器​​,下面就是示例

客户端javascript

   function getChart() {

        //first json object
        //note: each object Property name must be the same as it is in the Models classes on    server side
        Category = {};
        Category.Name = "Category1";
        Category.Values = [];
        Category.Values[0] = "CategoryValue1";
        Category.Values[1] = "CategoryValue2";

        //second json object
        XAxis = {};
        XAxis.Name = "XAxis1";
        XAxis.Values = [];
        XAxis.Values[0] = "XAxisValue1";
        XAxis.Values[1] = "XAxisValue2";

        //third json object
        YAxis = {};
        YAxis.Name = "YAxis1";

        //convert all three objects to string
        //note: each object name should be the same as the controller parameter is!!
        var StringToPost = JSON.stringify({CategoryObject : Category, XAxisObject : XAxis, YAxisObject : YAxis});

        $.ajax({
            url: "@Url.Action("getChart","SBM")",
            type: 'POST',
            contentType: "application/json",
            dataType: 'html',
            data: StringToPost,
            success: function (data) {
                var imagestring = btoa(data);
                $('#ChartImage').html(data);
            }
        })
    }
Run Code Online (Sandbox Code Playgroud)

MVC控制器

[HttpPost]
public ActionResult getChart(Category CategoryObject, XAxis XAxisObject, YAxis YAxisObject)
{
    //do some stuff with objects here and return something to client
    return PartialView("_Chart");
}
Run Code Online (Sandbox Code Playgroud)

分类模型

public class Category
{
    public string Name { get; set; }
    public List<string> Values { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

XAxis模型

public class XAxis
{
    public string Name { get; set; }
    public List<string> Values { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

YAxis模型

public class YAxis
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

希望它能帮助某人澄清整个画面!