2017-01-23 76 views
2

編輯:謝謝你抓住這些錯誤灰 有3個問題在這裏MVC通JSON數據到控制器

1. The JSON request was too large to be deserialized I had to add <add key="aspnet:MaxJsonDeserializerMembers" value="150000" /> in the app settings 
2. I forgot the getters and setters in one of my classes 
3. I misspelled one of my properties 

我想一些JSON數據傳遞給控制器​​。當我將js Object傳遞給Controller時,我的模型爲空。不應該使用默認模型綁定器來處理這個問題嗎?將數據傳遞給控制器​​時,我找不到爲什麼我得到空值。我看過其他的SO問題,但目前爲止沒有任何幫助。

的數據看起來像這樣

{ 
Data: [{ 
     duration: 5, 
     end_date: "06-04-2013 00:00" 
     id: 1, 
     open: true, 
     parent: 0, 
     progress: 0, 
     start_date: "01-04-2013 00:00", 
     text : "PcB ddCcgoMordiF Arr e" 
     }] 
Links: //empty array of something similar 
} 

這些是我的DTO的樣子

public class GanttRequestDto 
{ 
    public IEnumerable<GanttTaskDto> Data; 
    public IEnumerable<GanttLinkDto> Links; 
} 



public class GanttTaskDto 
    { 
     public int Id { get; set; } 
     public string Test { get; set; } 
     public DateTime Start_date { get; set; } 
     public DateTime End_date { get; set; } 
     public int Duration { get; set; } 
     public bool Open { get; set; } 
     public decimal Progress { get; set; } 
     public int? ParentId { get; set; } 
} 

public class GanttLinkDto 
{ 
    public int Id { get; set; } 
    public string Type { get; set; } 
    public int Source { get; set; } 
    public int Target { get; set; } 
} 

我的控制器看起來像這樣

[HttpPost] 
public BetterJsonResult SaveGanttChartData(GanttRequestDto ganttDataModel) 
{ 
    //do something 
    return null; 
} 

我的JS代碼

InitSaveButton() { 
    $("#save-btn").click(function() { 
     var ganttData = gantt.serialize(); 
     var model = { 
      Data: ganttData.data, 
      Links: ganttData.links 
     }; 
     Ajax.ajaxRequest(null, "/Gantt/SaveGanttChartData?ganttDataModel", model, null, "Saving Gantt Data", "Success", null); 
    }); 
} 

這裏是我的Ajax請求的樣子

//url:string 
//model:json object 
//updateId (optional): area to update, 
//toastMessage: optional toast message 
//toasTitle: optional toast title 
//onComplete: optional callback function 
Ajax.ajaxRequest = function (httpVerb, url, model, updateId, toastMessage, toastTitle, onComplete) { 
    if (httpVerb === null || httpVerb.length === 0) httpVerb = "POST"; 
    $.ajax({ 
     url: url, 
     type: httpVerb, 
     cache: false, 
     data: JSON.stringify(model), 
     dataType: "json", 
     contentType: 'application/json; charset=utf-8' 
    }).done(function (data) { 
     Ajax.ajaxSuccess(data, updateId, toastMessage, toastTitle); 
    }).fail(function (err) { 
     Ajax.ajaxFailure(err); 
    }).always(function (data) { 
     if (onComplete && typeof onComplete === 'function') { 
      onComplete(data); 
     } 
    }); 
}; 
+0

你的控制器BetterJsonResult返回null,這就是爲什麼你的對象沒有填充,返回ganttDataModel – Fuzzybear

+0

@Fuzzybear對不起,但這不是它,它不返回任何東西,這就是爲什麼我把null。無論我返回什麼,'ganttDataModel'都是'null'。 –

+0

@Ash我爲什麼要那樣做? –

回答

1

這裏是你的問題的修補程序。這裏

兩件事情:

  1. 我假設你提供的JSON樣本數據是定製的純手工,但仍提的是,數據是有幾個錯別字。

    var data = { 
    Data: [{ 
         duration: 5, 
         end_date: "06-04-2013 00:00", 
         id: 1, 
         open: true, 
         parentId: 0, 
         progress: 0, 
         start_date: "01-04-2013 00:00", 
         test : "PcB ddCcgoMordiF Arr e" 
         }], 
    Links: [{Id : 1, Type: "sdsd"}] 
    } 
    
  2. 您尚未將數據成員標記爲具有獲取者和設置者的屬性。

    public class GanttRequestDto 
    { 
        public IEnumerable<GanttTaskDto> Data { get; set; } 
    
        public IEnumerable<GanttLinkDto> Links { get; set; } 
    } 
    

現在好了嘗試,讓我知道它是否適合你與否。

+1

好主!尼斯漁獲。我忘記了吸氣劑和安裝者而我的財產拼寫錯了。 '公共字符串測試{get;組; }'而不是'public string Text {get;組; }'以及'JSON'數據太大而無法序列化。 –