Search code examples
javascriptc#jqueryajaxwebmethod

Optional Parameters in Web Method


I've defined the following web method:

public string GetMatchingCompanies(string term, int companyPickerMode, int? officeId, int? proposalId)
{
    // ...
}

As you can see, the last two arguments are nullable. The idea is that these arguments are optional. They will be null if they are not specified.

However, when my AJAX code calls this method without supplying one of these arguments, I get the following error.

Invalid web service call, missing value for parameter: 'officeId'.

This is unexpected. Isn't there a way to make these parameters optional?


Solution

  • Making then fields nullable does not mean that they do not need to be supplied, only that they will be initialized or can be set to null. If you do not want to specify them, set a default value like so:

    public string GetMatchingCompanies(string term, int companyPickerMode, int? officeId = null, int? proposalId = null)
    {
        // ...
    }