arman radan
arman radan

Reputation: 190

Can't pass complex parameters from jquery post to WebAPI

I have a ASP MVC project with WebAPI functions . I am trying to call those APIs through jquery POST request in my 'Tools for apache cordove' project . (visual studio 2015) it works for most functions . but I have one function with complex parameters :

public JsonResult LoadHomePage(MostGoodsViewer mostgoodsviewer, SimilarItems newsitems, SimilarItems reviewitems, SimilarItems imaginalitems, string subDomain)

All parameters are complex (except the last one) . for example :

public class MostGoodsViewer
{
    public MostGoodsViewer()
    {
        ListGoodTypeId = new List<string>();
        listPropertySetID = new List<int?>();
    }
    public List<string> ListGoodTypeId { get; set; }
    public List<int?> listPropertySetID { get; set; }
    public int PageSize { get; set; }
}

Now I want to call this API . first of all , I declare my parameters :

var mostgoodsviewerP =
    {
        ListGoodTypeId: [],
        listPropertySetID: [],
        PageSize: 13
    };
var newsitemsP =
    {
        ItemCodeText: "/",
        ItemTypeId: 15,
        Count: 9
    };
var reviewitemsP =
  {
      ItemCodeText: "/",
      ItemTypeId: 19,
      Count: 9
  };
var imaginalitemsP =
  {
      ItemCodeText: "/",
      ItemTypeId: 20,
      Count: 9
  };

Next, I use POST method :

xhr = $.post('https://localhost:44302/Home/LoadHomePage', {
        mostgoodsviewer: JSON.stringify(mostgoodsviewerP),
        newsitems: JSON.stringify(newsitemsP),
        reviewitems: JSON.stringify(reviewitemsP),
        imaginalitems: JSON.stringify(imaginalitemsP),
        subDomain: 'electronic'

    },
    function (data) {
      //  for (var i = 0; i < 9; i++)
        {
            var a = data._goodsorderByTotalVisit;
            // other stuff
        }
    }).fail(function (data) {
        alert("error");
    });

I also tried using jQuery.param instead of JSON.stringify but no difference . it always jumps into fail method with data "Object reference not set to an instance of an object".

Then I tried using Fiddler program to send request instead of my cordova project and running my MVC project with a breakpoint under function's header. What I realized is parameters are not correct . for example, integers are all zero . I don't know what to do now !

Upvotes: 0

Views: 156

Answers (1)

Parth Trivedi
Parth Trivedi

Reputation: 3832

You should do like

Jquery:

xhr = $.post('https://localhost:44302/Home/LoadHomePage', {
        mostgoodsviewer: JSON.stringify(mostgoodsviewerP),
        newsitems: JSON.stringify(newsitemsP),
        reviewitems: JSON.stringify(reviewitemsP),
        imaginalitems: JSON.stringify(imaginalitemsP),
        subDomain: 'electronic'

    },

Controller

public JsonResult LoadHomePage(string mostgoodsviewer, string newsitems, string reviewitems, string imaginalitems, string subDomain){

   MostGoodsViewer mostGoodsViewer = JsonConvert.DeserializeObject<MostGoodsViewer>(mostgoodsviewer);
    // and for others same thing to follow
}

may this will help you to solve this issue.

You can also do

$.post(url, data, function(), "json") 

Here add "json" as second arg and may your model bind issue fixed with your current code.

Upvotes: 1

Related Questions