Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am calling web-api method delete all with array type parameter, showing the value null. why?

I am passing data like : data: "ArrMenuId"+ JsArrayMenuId,

function performalldeletemenu()
{

    if (confirm('Are you sure you want to delete this menu?'))
    {
        var JsArrayMenuId = new Array();
        $("input:checked").each(function ()
        {
            //console.log($(this).val()); //works fine
            JsArrayMenuId.push($(this).val());
        });
        alert(JsArrayMenuId);
        $.ajax({
            url: '/api/MenuWebApi/DeleteAllMenu/',
            type: 'DELETE',
            contentType: 'application/json; charset=utf-8',
            data: "ArrMenuId"+ JsArrayMenuId,
            success: function (data)
            {
                if (data.Success == true)
                {
                    //GetMenuList();
                }
            },
            error: function (xhr, textStatus, errorThrown)
            {
                //window.location = JsErrorAction;
            },

            headers:
            {
                'RequestVerificationToken': JsTokenHeaderValue
            }
        });
    }
    return false;
}



public HttpResponseMessage DeleteAllMenu(Array ArrMenuId)
{

}

Here ArrMenuId is showing null values.

if any one have solution, please let me know.

share|improve this question
    
What does your delete method look like in your web api ? please add that to your question. You must be passing data in the wrong way, its very picky.. look here on how to pass data - api.jquery.com/jQuery.ajax –  krilovich Aug 13 '13 at 17:22
    
I am passing data like : data: "ArrMenuId"+ JsArrayMenuId, I have added method name + jquery ajax function call in my question. –  dotnetexpert Aug 13 '13 at 17:27
    
I have also checked using this : data: {ArrMenuId : JsArrayMenuId}, but same issue. please help me on it –  dotnetexpert Aug 13 '13 at 17:37
    
I am not sure you can pass an array like that from javascript to your controller. Try changing your controller method to DeleteAllMenu(string ArrMenuId) and doing data: {ArrMenuId : JsArrayMenuId.join()} in your javascript –  krilovich Aug 13 '13 at 17:38
1  
Hi if i am doing your way it is showing 404 error. DeleteAllMenu(string ArrMenuId) -> DeleteAllMenu(MenuModel ArrMenuId) if i am passing model then it is working and also your suggestion .join{} is good ...Thanks for your help MR krilovich . –  dotnetexpert Aug 13 '13 at 17:47

1 Answer 1

up vote 1 down vote accepted

Try changing

data: "ArrMenuId"+ JsArrayMenuId,

to

data: {ArrMenuId : JsArrayMenuId.join()} 

and changing

public HttpResponseMessage DeleteAllMenu(Array ArrMenuId)

to

public HttpResponseMessage DeleteAllMenu(string ArrMenuId)

I don't think javascript array will translate easily into a c# array and by changing it to this you are instead passing a string. Once you have this comma delimited string you can make it into an array in your c#

share|improve this answer
    
All is correct with your suggestion, just need to pass model with controller, string type showing 404 error. –  dotnetexpert Aug 13 '13 at 17:49
    
no problem :) if it helped you please upvote and accept –  krilovich Aug 13 '13 at 18:00
    
Yes, my problem solved thanks... –  dotnetexpert Aug 13 '13 at 18:20
    
Hi, I have another issue if you have any idea. stackoverflow.com/questions/18213485/… –  dotnetexpert Aug 13 '13 at 18:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.