1

I am trying to pass an id and an object which has four properties to an action controller but unfortunately it is not getting through and I get the following error.

The parameters dictionary contains a null entry for parameter 'batchID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult GetItems(Int32, Ebiquity.Reputation.Neptune.Web.Models.MyWorkFilterModel)' in 'Ebiquity.Reputation.Neptune.Web.Controllers.MyWorkController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters

The JSON being passed is:

{
    "batchID": 21610,
    "filter":
    {
        "issueNo": "1",
        "messageNo": "2",
        "itemDate": "Wed, 05 Feb 2014 00:00:00 GMT",
        "mediaName":"3"
    }
};

The ajax call:

self.GetItems = function (batchID, issueNo, messageNo, itemDate, mediaName) {

    var filter = {
        issueNo: issueNo,
        messageNo: messageNo,
        itemDate: itemDate,
        mediaName: mediaName
    };
        
    $.ajax({
        type: "GET",
        url: "/MyWork/GetItems",
        data: JSON.stringify({
            batchID: batchID,
            filter: filter
        }),
        dataType: "json",
        success: function (result) {

            self.Items([]);
            if (result.Items != null) {
                var tempItems = ko.mapping.fromJS(result.Items, mappingOptions);
                self.Items.push.apply(self.Items, tempItems());
            }
        }
    });
};

The controller:

[HttpGet]
public JsonResult GetItems(int batchID, MyWorkFilterModel filter)
{
    using (var rep = new WorkRepository(CurrentUser))
    {
        return Json(rep.GetBatch(batchID, filter), JsonRequestBehavior.AllowGet);
    }
}

The filter model:

public class MyWorkFilterModel
{
    public int? IssueNo { get; set; }
    public int? MessageNo { get; set; }
    public string MediaName { get; set; }
    public DateTime? ItemDate { get; set; }
}
Community
  • 1
  • 1
Farhad-Taran
  • 6,282
  • 15
  • 67
  • 121

6 Answers6

2

You have to change type of your AJAX call to POST instead of GET. Data parameters will not be send with GET requests.

semao
  • 1,757
  • 12
  • 12
2

Looks like your batchID is null. Either set it as 0 in javascript or make it nullable in action

[HttpGet]
public JsonResult GetItems(int? batchID, MyWorkFilterModel filter)
{

}
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0

We have a lot of discussions here like this (see the below link)

Posting JSON data via jQuery to ASP .NET MVC 4 controller action

Try like below Ajax DataType:

self.GetItems = function (batchID, issueNo, messageNo, itemDate, mediaName) {

    var filter = {
        issueNo: issueNo,
        messageNo: messageNo,
        itemDate: itemDate,
        mediaName: mediaName
    };

    $.ajax({
        type: "POST",
        url: "/MyWork/GetItems",
        dataType: 'text',
        data: "param=" + JSON.stringify({
            batchID: batchID,
            filter: filter
        }),
        success: function (result) {
            self.Items([]);
            if (result.Items != null) {
                var tempItems = ko.mapping.fromJS(result.Items, mappingOptions);
                self.Items.push.apply(self.Items, tempItems());
            }
        }
    });
};

The controller

[HttpPost]
public JsonResult GetItems(String Param)
{

}
Community
  • 1
  • 1
Anand Thangappan
  • 3,068
  • 2
  • 28
  • 31
0

You may try to make IssueNo and MessageNo as integers instead of strings. And use POST instead of GET.

If you have to use GET, you may pass the parameters as a part of the url only!

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
0

As mentioned by Semao, if you want that kind of JSON you need to use POST. Since your posting the JSON in that way, I guess you just have a typo and intend to use POST.

If you really want GET, you need to pass the values in the URL.

In your case, youneed to add:

[FromUri]

before the objects.

Example:

public class QueryObj
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public Result Get([FromUri] QueryObj queryObj)
{...}

http://.../query?Id=1&Name=test
user373455
  • 12,675
  • 4
  • 32
  • 46
0

I was missing the contenttype property of the ajax call and instead of GET I used POST and it worked.

$.ajax({
            type: "POST",
            url: "/MyWork/GetItems",
            data: JSON.stringify({
                batchID: batchID,
                filter: filter
            }),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                }
        });
Farhad-Taran
  • 6,282
  • 15
  • 67
  • 121