0

I have this ajax call and I am passing an array with it that looks like so ["ANC0001-Pin Lot"] and I am trying to pass it to asp.net. I can see in the network console under 'Request Payload' my array is there.

Here is the jquery:

$.ajax({
    type: "POST",
    url: "/vendorProject/api/connection/updateVendorItem",
    data: JSON.stringify({ edittedItems: editHolder }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        alert('success');
    },
    failure: function (errMsg) {
        alert('failed');
    }
});

and here is my ASP.NET Controller:

public List<VendorUpdateClass> updateVendorItem(Array edittedItems)
{
    ConnectionClass jobs = new ConnectionClass();
    return jobs.updateVendors(edittedItems);
}

updateVendors method:

 public List<VendorUpdateClass> updateVendors(Array items)
{
    VendorUpdateCell = new List<VendorUpdateClass>();
    VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();

    for (int i = 0; i < items.Length; i++ ){
        vendorUpdatedItem.job = "aaa";
        vendorUpdatedItem.task = "bbb";
        vendorUpdatedItem.vendor = "ccc";

        VendorUpdateCell.Add(vendorUpdatedItem);
    }

    return VendorUpdateCell;
}

I am getting and 500 Internal Server Error:

    {  
       "Message":"An error has occurred.",
       "ExceptionMessage":"Object reference not set to an instance of an object.",
       "ExceptionType":"System.NullReferenceException",
       "StackTrace":"   at VendorProject.Models.ConnectionClass.updateVendors(Array items)\r\n   
at lambda_method(Closure , Object , Object[] )\r\n   
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary 2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n   
at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n   
at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n   at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   
at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n   
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
    }

Without the loop this does work. When I put in the loop, this gives me a 500 error. Why is it doing this and how can I fix this?

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
user979331
  • 11,039
  • 73
  • 223
  • 418

3 Answers3

1

Try this:

public List<VendorUpdateClass> updateVendorItem(string[] edittedItems)
    {
        ConnectionClass jobs = new ConnectionClass();
        return jobs.updateVendors(edittedItems);
    }

You also could try replacing the string array with a List<string>

I also found this How can I post an array of string to ASP.NET MVC Controller without a form?

Maybe you could get some inspiration there

Community
  • 1
  • 1
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67
1

Right now you're posting an object that contains a property called edittedItems (side note: you probably meant editedItems with one "t") whose value is an array.

Contrast this with your controller action which expects just an array. You need to change your AJAX request to just POST an array instead:

data: JSON.stringify(editHolder)

And then as @JOSEFtw mentions, you should probably use a string[] or List<string> instead of Array.

Community
  • 1
  • 1
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
0

Now try this method, i have updated id, becuase i could be crash due to null reference items.Length

public List<VendorUpdateClass> updateVendors(Array items)
        {
            VendorUpdateCell = new List<VendorUpdateClass>();
            VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();

            if (items == null && items.Length < 1)
                return VendorUpdateCell;

            for (int i = 0; i < items.Length; i++)
            {
                vendorUpdatedItem.job = "aaa";
                vendorUpdatedItem.task = "bbb";
                vendorUpdatedItem.vendor = "ccc";

                VendorUpdateCell.Add(vendorUpdatedItem);
            }

            return VendorUpdateCell;
        }

write this line before for loop

if (items == null && items.Length < 1) return VendorUpdateCell;