ASP.NET mvc 4 控制器参数在将 json 发送到控制器时始终为空,为什么? [英] ASP.NET mvc 4 controller parameter always null when sending json to controller, why?

查看:22
本文介绍了ASP.NET mvc 4 控制器参数在将 json 发送到控制器时始终为空,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There are some similar posts already here, and tried every solution suggested, and still does not work... I can not get value inside controller, it is always null. Below is the code. Am I missing something?

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

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

推荐答案

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

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!

这篇关于ASP.NET mvc 4 控制器参数在将 json 发送到控制器时始终为空,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆