0

.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
  • Look at the client-side content and add to your post the data assigned to the check javascript variable. Commented Jul 25, 2014 at 18:15

1 Answer 1

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}

    }
Sign up to request clarification or add additional context in comments.

1 Comment

I found the culprit "contentType: 'application/json; charset=utf-8'," which is expecting Json, for that i am using JSON.Stringify() which is converting Date into Json Format which is not identified by Controller Action.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.