gpt4 book ai didi

asp.net-mvc - ASP.NET mvc 4 Controller 参数在将 json 发送到 Controller 时始终为空,为什么?

转载 作者:行者123 更新时间:2023-12-03 20:04:26 27 4
gpt4 key购买 nike

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

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

最佳答案

谢谢各位大侠的指点和解决方法。解决方案是您所有建议的组合,因此我决定将其汇总在一篇文章中。

问题的解决方法如下:

  • contentType应该是 application/json (如 Ant P 上面建议的那样)
  • json 数据应采用 JSONString3 = {"Name" : "monday" } 的形式(如 Ant P 上面建议的那样)
  • 在发送到 Controller 之前,json 应该是 stringifyed如下:JSONString3 = JSON.stringify(JSONString3) (正如全建议的那样)

  • 客户端 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 Controller
    [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";

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

    To pass multiple objects to controller, below is the example



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

    希望它会帮助有人澄清整个画面!

    关于asp.net-mvc - ASP.NET mvc 4 Controller 参数在将 json 发送到 Controller 时始终为空,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384242/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com