[英]ASP.NET mvc 4 controller parameter always null when sending json to controller, why?
這里已經有一些類似的帖子,並嘗試了所有解決方案,但仍然無效......我無法在控制器中獲取值,它始終為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;
}
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; }
}
謝謝你們的指示和解決方案。 解決方案是您所有建議的組合,因此我決定將其整理到一個帖子中。
解決問題的方法如下:
contentType
應該是application/json
(如上面建議的Ant P) JSONString3 = {"Name" : "monday" }
(如上面提到的Ant P所示) 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;
}
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";
但我們仍然需要在發布到控制器之前對對象進行字符串化!
要將多個對象傳遞給控制器,下面就是示例
客戶端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; }
}
XAxis模型
public class XAxis
{
public string Name { get; set; }
public List<string> Values { get; set; }
}
YAxis模型
public class YAxis
{
public string Name { get; set; }
}
希望它能幫助某人澄清整個畫面!
我有同樣的問題(參數總是為null),但我的解決方案是不同的。
確保您的ActionResult方法參數與JSON對象屬性的名稱不同。
在這個例子中,我將myParam重命名為myNewParam,以區別於MyParam屬性。
示例:這不起作用:
var myObj = {
ID: '0',
MyParam: $('#mycontrol').val(),
};
$.ajax({
type: "POST",
url: '@Url.Action("MyAction", "MyModel")',
cache: false,
data: JSON.stringify(myObj),
datatype: 'json',
contentType: "application/json; charset=utf-8",
success: function (result) {
}
})
[HttpPost]
public ActionResult MyAction(Class1 myParam)
這將有效:
var myObj = {
ID: '0',
MyParam: $('#mycontrol').val(),
};
$.ajax({
type: "POST",
url: '@Url.Action("MyAction", "MyModel")',
cache: false,
data: JSON.stringify(myObj),
datatype: 'json',
contentType: "application/json; charset=utf-8",
success: function (result) {
}
})
[HttpPost]
public ActionResult MyAction(Class1 myNewParam) -->renamed
在我看來,你正試圖傳遞一系列對象:
JSONString3 = { HAxis : [{ Name : "monday" }] };
當你的行動只想要一個:
public ActionResult getChart(YAxis HAxis)
也許你只想傳遞一個?
JSONString3 = { "Name": "monday" };
JSONString3 = { "Name": "monday" };
你應該把它作為一個字符串發布到控制器,所以使用JSON.stringify進行轉換,我不知道如何使用你的ajax類型,我只知道使用$ .post ... T_T
$.post('@Url.Action("getChart","SBM")', {yourJson : data:JSON.stringify(JSONString3)} , function(data) {
if (data.success) {
var imagestring = btoa(data.name);
$('#ChartImage').attr('src', "data:image/png;base64," + imagestring + "?" + new Date().getTime());
}
});
在控制器中,
public ActionResult getChart(string yourJson)
{
YAxis yAxis= JsonConvert.DeserializeObject<YAxis>(yourValue);
// ....... your code here
return Json(new{success=true,name=yAxis.Name},JsonRequestBehavior.AllowGet);
}
**注意:JsonConvert是使用Newtonsoft.Json的方法; ,請添加Newtonsoft參考。
向控制器方法添加數據類型屬性為我解決了這個問題。
[JsonFilter(Param="yourParamName", JsonDataType=typeof(YourParamType))]
[HttpPost]
public ActionResult yourFunction(YourParamType YourParamName)
{
//do some stuff
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.