Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.ajax post to MVC controller returns Internal Server Error: Parameters dictionary contains null entry

The parameters dictionary contains a null entry for parameter 'appId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ContentResult CheckForInstaller(Int32)' in 'HLIT_TicketingMVC.Controllers.TicketController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

function SubmitAjax(url, message, successFunc, errorFunc) {
  $.ajax({
    type:'POST',
    url:url,
    data:message,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success:successFunc,
    error:errorFunc
  });
};

The data object is built as follows:

var message={"appId":application.val()};

I have also tried a jsonified string:

var message="{'appId':"+application.val()+"}";

and

var message="{'appId':'"+application.val()+"'}";

I validated that the message is coming through with a proper int value before it tries to post. The mouse over debugger most recently showed: {appId="6"}

The method signature on the controller is:

public ContentResult CheckForInstaller(int appId)

When I remove the parameter from the method signature, it does hit the breakpoint inside, so it's either the signature needing attributes of some kind, or the message isn't built properly I believe.

like image 914
Maslow Avatar asked Feb 17 '10 15:02

Maslow


2 Answers

Remove this:

contentType: 'application/json; charset=utf-8',

MVC isn't going to parse the JSON into an int. You want the default value of application/x-www-form-urlencoded.

like image 137
Craig Stuntz Avatar answered Nov 07 '22 01:11

Craig Stuntz


I think it maybe that you are sending json to the controller try this

function SubmitAjax(url, message, successFunc, errorFunc) {
  $.ajax({
    type:'POST',
    url:url,
    data:"appId=" + application.val(),//not sure where you get the value from in your current code  
    dataType: 'json',
    success:successFunc,
    error:errorFunc
  });
};
like image 23
Pharabus Avatar answered Nov 07 '22 02:11

Pharabus