ASP.NET MVC 4控制器参数总是空发送JSON时,控制器,为什么呢? [英] ASP.NET mvc 4 controller parameter always null when sending json to controller, why?
问题描述
有一些类似的帖子已经在这里,并试图每一个解决方案建议,仍然没有工作...我不能让内部控制值时,它始终为空。下面是code。我缺少的东西吗?
客户端的JavaScript
函数getChart(){
JSONString3 = {HAxis:[{名称:星期一}]};
jQuery.ajaxSettings.traditional = TRUE;
$阿贾克斯({
网址:@ Url.Action(getChart,SBM),
输入:POST,
的contentType:JSON,
数据类型:HTML,
数据:JSONString3,
成功:功能(数据){
变种和imagestring = BTOA(数据);
$('#ChartImage')ATTR。('src'中,:;?,数据图像/ PNG的base64,+和imagestring + +新的Date()的getTime());
}
})
jQuery.ajaxSettings.traditional = FALSE;
}
MVC控制器
[授权]
[HttpPost]
公众的ActionResult getChart(Y轴HAxis)
{
Y轴XAxisvalue = HAxis;
图表图表=新图();
MemoryStream的毫秒=新的MemoryStream();
chart.Chart.SaveImage(毫秒);
字符串图像= Convert.ToBase64String(ms.GetBuffer());
返回文件(ms.GetBuffer(),图像/ PNG,Chart.png);
}
型号
公共类Y轴
{
公共字符串名称{;组; }
}
谢谢你的方向和解决方案的家伙。解决的办法是你的所有建议的组合,所以我决定舍它在一个职位。
解的问题如下:
-
的contentType
应应用程序/ JSON
(蚂蚁P的上方建议) - JSON数据应该在
JSONString3 = {姓名:星期一}的形式
(蚂蚁P的上方建议) - 作为全建议)
JSONString3 = JSON.stringify(JSONString3)
(:发送给控制器,JSON应 stringifyed
如下之前客户端的JavaScript
函数getChart(){
JSONString3 = {姓名:星期一};
jQuery.ajaxSettings.traditional = TRUE;
$阿贾克斯({
网址:@ Url.Action(getChart,SBM),
输入:POST,
的contentType:应用/ JSON,
数据类型:HTML,
数据:JSON.stringify(JSONString3)
成功:功能(数据){
变种和imagestring = BTOA(数据);
$('#ChartImage')ATTR。('src'中,:;?,数据图像/ PNG的base64,+和imagestring + +新的Date()的getTime());
}
})
jQuery.ajaxSettings.traditional = FALSE;
}
MVC控制器
[授权]
[HttpPost]
公众的ActionResult getChart(Y轴HAxis)
{
Y轴XAxisvalue = HAxis;
图表图表=新图();
MemoryStream的毫秒=新的MemoryStream();
chart.Chart.SaveImage(毫秒);
字符串图像= Convert.ToBase64String(ms.GetBuffer());
返回文件(ms.GetBuffer(),图像/ PNG,Chart.png);
}
型号
公共类Y轴
{
公共字符串名称{;组; }
}
而不是这样的:
JSONString3 = {姓名:星期一};
我们可以这样做:
VAR JSONString3 = {};
JSONString.Name =星期一;
但是,我们仍然需要发布到控制器之前,字符串化的对象!
的 要传递多个对象控制器,下面是例子的
块引用>客户端的JavaScript
函数getChart(){ //第一个JSON对象
//注意:每个对象属性名必须是相同的,因为它是在模型类服务器端
分类= {};
Category.Name =组别;
Category.Values = [];
Category.Values [0] =CategoryValue1;
Category.Values [1] =CategoryValue2; //第二JSON对象
XAXIS = {};
XAxis.Name =XAxis1;
XAxis.Values = [];
XAxis.Values [0] =XAxisValue1;
XAxis.Values [1] =XAxisValue2; //第三JSON对象
Y轴= {};
YAxis.Name =YAxis1; //转换所有三个对象为字符串
//注意:每个对象的名称应该是一样的控制器参数!
VAR StringToPost = JSON.stringify({CategoryObject:类别,XAxisObject:X轴,YAxisObject:Y轴}); $阿贾克斯({
网址:@ Url.Action(getChart,SBM),
输入:POST,
的contentType:应用/ JSON
数据类型:HTML,
数据:StringToPost,
成功:功能(数据){
变种和imagestring = BTOA(数据);
$('#ChartImage')的html(数据)。
}
})
}MVC控制器
[HttpPost]
公众的ActionResult getChart(分类CategoryObject,XAXIS XAxisObject,Y轴YAxisObject)
{
//做一些东西在这里对象,并返回一些客户
返回PartialView(_表);
}分类模型
公共类分类
{
公共字符串名称{;组; }
公开名单<串GT;值{搞定;组; }
}XAXIS模型
公共类XAXIS
{
公共字符串名称{;组; }
公开名单<串GT;值{搞定;组; }
}Y轴模型
公共类Y轴
{
公共字符串名称{;组; }
}希望这将帮助别人澄清全貌!
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?
Client side 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"); }
Model
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.
Solution to the problem is as follows:
contentType
should beapplication/json
(as Ant P suggested above)- json data should be in form of
JSONString3 = {"Name" : "monday" }
(as Ant P suggested above)- before sending to controller, json should be
stringifyed
as follows:JSONString3 = JSON.stringify(JSONString3)
(as Quan suggested)Client side 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"); }
Model
public class YAxis { public string Name { get; set; } }
Instead of this:
JSONString3 = { "Name" : "monday" };
we can do this:
var JSONString3 = {}; JSONString.Name = "monday";
But we still need to stringify object before posting to controller!!!
To pass multiple objects to controller, below is the example
Client side 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"); }
Category model
public class Category { public string Name { get; set; } public List<string> Values { get; set; } }
XAxis model
public class XAxis { public string Name { get; set; } public List<string> Values { get; set; } }
YAxis model
public class YAxis { public string Name { get; set; } }
Hope it will help someone to clarify the whole picture!
这篇关于ASP.NET MVC 4控制器参数总是空发送JSON时,控制器,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!