weixin_33727510 2014-07-25 18:12 采纳率: 0%
浏览 28

MVC 4 Ajax Json传递模型

.I am trying to post my view model through ajax as below:

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");
    }
});
});

My Controller Action is

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;
        }

Just to let you know its a complex model but ajax is passing model correctly, apart from date.

Date value DateTime.MinValue (which is incorrect). When I look at the date in client side it is there in Json Format.

I think some how it cannot able to Deserialize just date. All the other objects are fine.

Any help will be appreciated.

Edit

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
  • 写回答

1条回答 默认 最新

  • YaoRaoLov 2014-07-28 11:19
    关注
    @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}
    
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面