2014-07-25 2 views
0

.I 아래로 아약스를 통해 내보기 모델을 게시하는 것을 시도하고있다 :MVC 4 AJAX JSON 전달 모델

var val = Json.Encode(Model); 
var check = @Html.Raw(val);  

$("#stats").click(function (e) { 
    e.preventDefault(); 
    console.debug(JSON.stringify(check)); 
    $.ajax({ 
    url: '/ReportingWall/analyseStats/', 
    type: 'POST', 
    data: JSON.stringify(check), 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function (data) { 
    alert("Sucess"); 
    } 
}); 
}); 

내 컨트롤러 액션은 그냥 그 복잡한 모델을 알려

public ContentResult AnalyseStats(ResponsiveReportViewModel model) 
     { 
      //JavaScriptSerializer js = new JavaScriptSerializer(); 

      //ResponsiveReportViewModel model = (ResponsiveReportViewModel)js.Deserialize(jsonPost, typeof(ResponsiveReportViewModel)); 

      if (model != null) 
      { 
       var sources = model.Sources.Where(x => x.Checked).Select(x => x.Id); 
       var terms = model.Terms.Where(x => x.Checked).Select(x => x.Id.ToString()); 
       if (terms.Count() > 0) 
       { 
        var graph = _reportingWallQueries.SetGraphData(terms, model.FromDate, model.ToDate); 
        if (graph != null) 
        { 
         string json; 
         var s = new JavaScriptSerializer(); 
         var sw = new StringBuilder(); 
         s.Serialize(graph, sw); 
         json = sw.ToString(); 
         return new ContentResult { Content = json, ContentType = "application/json" }; 
        } 
       } 
      } 
      return null; 
     } 

입니다 하지만 Ajax는 날짜를 제외하고 모델을 올바르게 전달하고 있습니다.

날짜 값 DateTime.MinValue (올바르지 않음) 클라이언트 측에서는 날짜를 볼 때 Json Format이 있습니다.

몇 가지 방법으로 날짜를 역 직렬화 할 수 없다고 생각합니다. 다른 모든 개체는 괜찮습니다.

도움이 될 것입니다. 클라이언트 측의 컨텐츠에

편집

Object {Id: null, DetectedDevice: "", Search: null, RequestUrl: ""} 
DataModel: Object 
DateTimePeriodDescription: null 
DetectedDevice: "" 
FromDate: "/Date(1405707399000)/" 
Id: null 
PageSize: 49 
ReportType: null 
RequestUrl: "" 
Search: null 
Sources: Array[5] 
Terms: Array[82] 
ToDate: "/Date(1406312199000)/" 
isResultsExits: true 
__proto__: Object 
+0

봐 및 게시물에 자바 스크립트 변수 검사에 할당 된 데이터를 추가 할 수 있습니다. –

답변

1
@using (Ajax.BeginForm("", "", null, new AjaxOptions() { HttpMethod = "POST" }, new { id = "frm" })) 
    { 
     <div id="hiddenFields"> 
      <input type="hidden" name="PageIndex" id="PageIndex" value="1" />    
     </div> 

    } 


    var dataPost = $("#frm").serialize(); 

    $.post("/controller/testmethod", dataPost, function (data) { 

    } 


    publick void (Mydata data){ 


    } 

    public class Mydata { 

    public PageIndex {get;set} 

    } 
+0

날짜를 JSON 형식으로 변환하는 JSON.Stringify()를 사용하고 있기 때문에 Json이 예상하고있는 "contentType : 'application/json; charset = utf-8' . –