2012-11-08 38 views
0

我試圖做一個控制器方法的AJAX調用參數是null無論我嘗試。我遵循了所有類似的SO帖子,但無濟於事。對不起,如果答案在那裏,我無法找到它。該代碼我是...Ajax調用控制器方法沒有傳遞參數

Ajax調用

var sguid = $(nTr).attr('id'); 
    $.ajax({ 
    url: "/Dashboard/Reporting/GetBlacklistedSoftwareItems", 
    type: 'POST', 
    dataType: 'json', 
    data: JSON.stringify({guid: sguid}), 
    statusCode: { 
     404: function() { 
    alert("page not found"); 
     } 
    }, 
    success: function (data) { 
    //DO Something 
    }, 
    error: function() { 
     alert("error"); 
    } 
}); 

控制器方法

public JsonResult GetBlacklistedSoftwareItems(string guid) 
{ 
    List<DeviceSoftware> ldevice = new List<DeviceSoftware>(); 
    Guid i = Guid.Parse(guid); 
    ReportMethods reportingMethods = new ReportMethods(); 
    ldevice = reportingMethods.GetNonCompliantApplicationReport(CompanyId); 

    DeviceSoftware ds = ldevice.Find(x => x.Device.Guid == i); 
    List<DeviceApplication> da = new List<DeviceApplication>(); 
    if (ds != null) 
    { 
     da = ds.DeviceApplications; 
    } 

    return Json(da, JsonRequestBehavior.AllowGet); 
} 

的方法被擊中它只是​​常是nullsguid確實持有我想傳遞的數據。 有人能告訴我我缺少什麼嗎?

+0

在你的$ .Ajax()調用中添加contentType:「application/json; charset = utf-8」,然後它會解決 – Desmond

回答

1

反對一切我讀我改變

data: JSON.stringify({guid: sguid}),

data: {guid: sguid},

現在的工作。

0

弗雷德,

你需要讓GetBlacklistedSoftwareItems POST方法....

試試這個...

[HttpPost] 公共JsonResult GetBlacklistedSoftwareItems(字符串GUID) {

0

需要做些小的改變。

var sguid = $(nTr).attr('id'); 
    $.ajax({ 
    url: "/Dashboard/Reporting/GetBlacklistedSoftwareItems", 
    contentType: "application/json; charset=utf-8" ,//This is very important 
    type: 'POST', 
    dataType: 'json', 
    Data: JSON. stringify ({guild: squid}), 
    statusCode: { 
     404: function() { 
    alert("page not found"); 
     } 
    }, 
    success: function (data) { 
    //DO Something 
    }, 
error: function() { 
    alert("error"); 
} 

});

添加的contentType: 「應用/ JSON的;字符集= UTF-8」,到$就調用。 :)

相關問題