2013-12-16 51 views
1

當我從Ajax調用收到JSON數據,我的型號是空:MVC3 - 傳遞的Json模型控制器(詞典)

這是我在C#模式:

public class ErrorReportModel 
{ 
    public bool isSuccess { get; set; } 
    public Dictionary<string, ScriptExecResultEntity[]> Errors{get;set;} 
} 

這是我的AJAX調用:

StringJsonLastExecutionReportModel = JSON.stringify(JsonLastExecutionReportModel); 
    $.ajax({ 
    url: urlPath, 
    type: 'POST', 
    dataType: 'html', 
    data: StringJsonLastExecutionReportModel, 
    contentType: 'application/json; charset=utf-8', 
    success: function (data, status, xhr) { 

    }, 
    error: function (xhr, status, error) { 
     alert(error); 
    } 
    }); 

JSON數據(StringJsonLastExecutionReportModel)看起來是這樣的,它是有效的:

{ 
    "isSuccess": false, 
    "Errors": { 
     "e794bfa7-657b-482c-acdf-c5a3b6d2ce83": [ 
      { 
       "ErrorMsg": "Invalid object name 'asfasf.CONTACT'.", 
       "Id": "63755f2a-1f02-480e-ab94-cafc62fd9634", 
       "ScriptContent": "CREATE VIEW [asfasf].Blala AS select * from [asfasf].[CONTACT]", 
       "ScriptDescription": "Script 6", 
       "ServerType": "SQLSERVER", 
       "Success": false 
      } 
     ] 
    } 
} 

這裏是我的控制器的方法:

public void DisplayErrorsReportAsync(ErrorReportModel model) 
    { 
     AsyncManager.Parameters["ErrorReportModelObject"] = model; 
    } 

接收模式是這樣的:

Received Model

爲什麼我在我的字典裏的值對象是空?

回答

1

你的Ajax調用

var json= "{\"isSuccess\": false, \"Errors\": { \"e794bfa7-657b-482c-acdf-c5a3b6d2ce83\": [  {    \"ErrorMsg\": \"Invalid object name 'asfasf.CONTACT'.\",  \"Id\": \"63755f2a-1f02-480e-ab94-cafc62fd9634\",    \"ScriptContent\": \"CREATE VIEW [asfasf].Blala AS select * from [asfasf].[CONTACT]\",   \"ScriptDescription\": \"Script 6\",  \"ServerType\": \"SQLSERVER\",  \"Success\": false  }  ] }}"; 

$.ajax({ 
     type: "POST", 
     url: '/Default1/DisplayErrorsReportAsync?json=' + json, 
     data: json, 
     dataType: 'json', 
     //contentType: false, 
     //processData: false, 
     success: function (response) { 
      alert(response); 
      $('#GeneralSection').html(response.responseText); 
     }, 
     error: function (error) { 
      $('#GeneralSection').html(error.responseText); 
     } 
    }); 

採取JSON作爲串在你的控制器

在控制器使用

[HttpPost] 
    public ActionResult DisplayErrorsReportAsync(string aaa) 
    {  
    JObject o = JObject.Parse(json); 
    JArray a = (JArray)o["ErrorReportModel"]; 
    ErrorReportModel model= a.ToObject<ErrorReportModel>(); 
    } 

注:使用Newtonsoft.Json

+0

什麼是jQuery的簽名.ajax()函數,以字符串而不是json傳遞對象。因爲現在我收到來自ajax調用的字符串對象作爲空 – billybob

+0

改變你的控制器方法,而不是模型放'字符串模型'然後接受它作爲字符串 – Nilesh

+0

我做了,但它不工作。我收到它爲空。 – billybob

相關問題