This is a migrated thread and some comments may be shown as answers.

Binding custom parameter map to WebAPI

2 Answers 666 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Dennis
Top achievements
Rank 1
Dennis asked on 27 Aug 2012, 11:17 AM
Hi, 

I've been having issues binding a custom parameter map to an ASP web api method. I basicly have a kendo grid with a delete button. But by default if I click the delete button it sends the whole data source model for the row as a request. I want it to send just the ID because thats all I need. While its set to the default and I have a viewModel as a input parameter for the web api delete method with the same properties as the data source model everything works fine. I wrote a parameter map that in case of destroy only sends the userID and observed the network request. The ID gets sent fine, but the web API method returns an error that the non nullable input parameter is a null. So it didn't bind right. Anybody had experience with this?

This is the important part of my data source, where you can see the model, parameter map and destroy method, I should note that I also tried using kendo stringify on the return for the parameter map that didnt help.

var viewModel = kendo.observable({
    teamMembersSource: new kendo.data.DataSource({
        transport: {
            read: {
                url: '@(Url.Action("Get", "TeamMembers", new { Area = "API" }))', 
                data: {
                    companyID: '@(((Signuper.WebApp.Infrastructure.SignuperProfessional)User).CompanyID)'
                }
            },
            destroy: {
                url: '@(Url.Action("Delete", "TeamMembers", new { Area = "API" }))',
                type: "DELETE",
                datatype: "json",
            },
            parameterMap: function (options, type) {
                if (type == "destroy") {
                    return { ID: options.UserID };
                }
                else
                    return options;
            }
        },
        schema: {
            model: {
                id: "UserID",
                fields:
                    {
                        UserID: { type: "number"},
                        FirstName: {type: "string"},
                        LastName: {type: "string"},
                        Gender: {type:"string"},
                        CompanyID: { type: "number" },
                        EmailAdress: { type: "string" },
                        IsCompanyAdmin: { type: "boolean" },
                        DateOfBirth: { type: "Date" }
                    }
            },
            data: "TeamMembers",
            total: "TotalCount"
        },


This is the code for my web Api method, pretty simple:
public dynamic Delete(long ID)
{
    var user = membership.GetProfessional(ID);
    if (user == null)
        return new { success = false, errors = "The user that is set to be deleted cannot be found" };
 
    if (ID == ((Signuper.WebApp.Infrastructure.SignuperUser)User).UserID)
        return new { success = false, errors = "You cannot delete the currently logged in user" };  // You cant delete your own user
 
    if (((Signuper.WebApp.Infrastructure.SignuperProfessional)User).CompanyID != user.CompanyID)
        return new { success = false, errors = "You cannot delete the user of a different company" };
 
    membership.DeleteUser(ID);
    return  new { success = true, errors = "You cannot delete the user of a different company" };
     
}

And I've attached a picture with my http request, the response is: {"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int64' for method 'System.Object Delete(Int64)' in 'Signuper.WebApp.Areas.API.Company.Controllers.TeamMembersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."} One final thing to note is that i first named the parameter on the web api method and in the parameter map UserID instead of ID as it is now. When it's like that I get a different webAPI error saying simply that no webAPI method that matches my parameters can be found.

Thanks in advance to anybody that can help!

2 Answers, 1 is accepted

Sort by
0
Dennis
Top achievements
Rank 1
answered on 27 Aug 2012, 11:32 AM
Ok, after spending days with this, just after posting i found out the problem, or at least a workaround solution :)

It seems to some sort of an WebAPI issue. I created a new model class and put the ID inside as a property. I put that
model class as a paramter for my web api method and everything works fine! Don't know why this seems to be a problem though
because I have a ton of other WebAPI methods that take primitive parameters like ints (the GETs for instance usually have parameters for paging) and those work just fine.

Kendo team have any info on this maybe? Just to know if this is a kendo issues I'm having or a WebAPI issue.
0
Ross
Top achievements
Rank 1
answered on 12 Sep 2012, 04:19 PM
Dennis, could you actually re-post your code for both the dataSource and the MVC controller so I can see how you did this?  I'm having the same issue also.
Tags
Data Source
Asked by
Dennis
Top achievements
Rank 1
Answers by
Dennis
Top achievements
Rank 1
Ross
Top achievements
Rank 1
Share this question
or