Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help binding json data to a controller action method - i'm stumped

I am missing something and I need some help. I am at my wits end. Everything looks correct, but I'm getting all null values for the model in the controller action method.

Any input would be appreciated. Thanks!

I have the following action method on my controller...

    [HttpPost]
    public JsonResult RunReport(ReportSubmitModel  model)            
    {

    }

I have the following model class...

   public class ReportSubmitModel
   {
    public string[] RequiredParameters { get; set; }

    public string[] Companies { get; set; }

    public string[] AssetTypes { get; set; }

    public string[] ExpenseStatuses { get; set; }

    public string StartDate { get; set; }

    public string EndDate { get; set; }

    public string ReportGuid { get; set; }
   }

Here is what my Json looks like by calling JSON.stringify() and outptting it to the console.

[{"ReportGuid":"1011e87e-074b-4a40-bea6-ed271ab1e7ca"},{"Companies":["434b5d92-c41e-40b7-8490-11ed0047b232","966b8291-ff4c-4254-a475-cdc1277d014d","775ce236-a17f-41f0-b254-415163217181","129de43b-6fb1-475e-a6d2-28143750c973","fec4475a-e992-4560-b9b1-efd486ad10ca"]},{"AssetTypes":["192a3095-b33f-474f-af02-557ad5cb69f6","192a3095-b33f-474f-af02-557ad5cb69f6","34e3b858-a682-4936-8e43-c663a4df2cd2"]},{"RequiredParameters":["C","AT","FS","CS","SCH"]},{"StartDate":"1/1/2012"},{"EndDate":"1/1/2013"},{"ExpenseStatuses":["1","2"]}] 

Here is what my ajax call looks like...

.ajax({
            type: "POST",
            data: JSON.stringify(postInfo),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            url: "/Home/RunReport",
            success: loadReport}); 

UPDATE: postInfo is an array that I am pushing values onto.

For example, postInfo.push({ReportGuid: "guidvalue"});

like image 681
RDotLee Avatar asked Aug 14 '26 21:08

RDotLee


1 Answers

It's because of all those extra curly brackets. I don't know how you built the javascript object but it should not be with {. With those in place, each item are treated as object and so you are in effect passing a collection object to your controller method. When your object is stringified it should look something like this:

{"ReportGuid": "1011e87e-074b-4a40-bea6-ed271ab1e7ca" ,
"Companies":["434b5d92-c41e-40b7-8490-11ed0047b232",
    "966b8291-ff4c-4254-a475-cdc1277d014d",
    "775ce236-a17f-41f0-b254-415163217181"]

Notice I removed the { before and after the Companies field. Actually you can build it like that in js, like this (I removed the other parts to simplify the example):

var postInfo = 
   {"ReportGuid": "1011e87e-074b-4a40-bea6-ed271ab1e7ca" ,
    "Companies":["434b5d92-c41e-40b7-8490-11ed0047b232",
        "966b8291-ff4c-4254-a475-cdc1277d014d"]};
like image 175
von v. Avatar answered Aug 17 '26 22:08

von v.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!