且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-25 16:58:58

感谢大家的指导和解决方案.解决方案是综合了您的所有建议,因此我决定将其汇总在一个帖子中.

Thank you guys for the directions and solutions. The solution is a combination of all of your suggestions, so I decided to round it up in one post.

问题的解决方法如下:

  1. contentType 应该是 application/json(如 Ant P 上面建议的那样)
  2. json 数据应该采用 JSONString3 = {"Name" : "monday" } 的形式(如 Ant P 上面建议的那样)
  3. 在发送到控制器之前,json 应该被stringify 如下:JSONString3 = JSON.stringify(JSONString3)(如 Quan 建议)
  1. contentType should be application/json (as Ant P suggested above)
  2. json data should be in form of JSONString3 = {"Name" : "monday" } (as Ant P suggested above)
  3. before sending to controller, json should be stringifyed as follows: JSONString3 = JSON.stringify(JSONString3) (as Quan suggested)

客户端 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;
    }

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

模型

public class YAxis
{
    public string Name { get; set; }
}

取而代之的是:

JSONString3 = { "Name" : "monday" };

我们可以这样做:

var JSONString3 = {};
JSONString.Name = "monday";

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

But we still need to stringify object before posting to controller!!!

将多个对象传递给控制器​​,示例如下

客户端 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);
            }
        })
    }

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

类别模型

public class Category
{
    public string Name { get; set; }
    public List<string> Values { get; set; }
}

X轴模型

public class XAxis
{
    public string Name { get; set; }
    public List<string> Values { get; set; }
}

Y轴模型

public class YAxis
{
    public string Name { get; set; }
}

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

Hope it will help someone to clarify the whole picture!