2

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.

5
  • 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 Commented Aug 13, 2013 at 17:22
  • I am passing data like : data: "ArrMenuId"+ JsArrayMenuId, I have added method name + jquery ajax function call in my question. Commented Aug 13, 2013 at 17:27
  • I have also checked using this : data: {ArrMenuId : JsArrayMenuId}, but same issue. please help me on it Commented Aug 13, 2013 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 Commented Aug 13, 2013 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 . Commented Aug 13, 2013 at 17:47

1 Answer 1

1

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#

Sign up to request clarification or add additional context in comments.

2 Comments

All is correct with your suggestion, just need to pass model with controller, string type showing 404 error.
Hi, I have another issue if you have any idea. stackoverflow.com/questions/18213485/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.