Dheeraj Palagiri
Dheeraj Palagiri

Reputation: 1879

MVC 4 ajax Json passing model

.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

Upvotes: 0

Views: 989

Answers (1)

LittleDragon
LittleDragon

Reputation: 2437

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

    }

Upvotes: 1

Related Questions