0
votes

I created an object like so:

Object {ABC-123: "XYZ", ABC-112: "LAX"}

and I am trying to send that object to .NET, but I am getting a 500 error, here is how I am sending this object:

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

the variable editObject is the object above. I don't think this part is the problem.

I am sending it to this controller in ASP.NET:

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

and then sending it this class:

public List<VendorUpdateClass> updateVendors(string[] items)
        {
            VendorUpdateCell = new List<VendorUpdateClass>();

            VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();

            for (int i = 0; i < items.Length; i++ ){
                vendorUpdatedItem.job = items[i];
                vendorUpdatedItem.task = items[i];
                vendorUpdatedItem.vendor = items[i];

                VendorUpdateCell.Add(vendorUpdatedItem);
            }

            return VendorUpdateCell;
        }

Now here is something interesting when I remove the loop from my class so it looks like so:

public List<VendorUpdateClass> updateVendors(string[] 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;
        }

this does not give me a 500 error, in fact I get the success alert from the ajax call. So I am assuming I am doing something wrong to loop through my object in .NET Does anyone have any suggestions ?

What I am expecting is vendorUpdatedItem.job equals ABC-123, vendorUpdatedItem.task equals ABC-123 and vendorUpdatedItem.vendor equals XYZ (for the first item anyways)

There is another problem...I can't debug my .NET code as my IIS is not working.

but here is the response I get from my network calls:

{"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(String[] items)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 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.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.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.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.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.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.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.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

1
Add the following code in your failure section and try to find out the exact error message: .failure(function (xhr, textStatus, errorThrown) { //what is returned in errorThrown? });Vimalan Jaya Ganesh
While I wouldn't necessarily expect it to cause an error, your initial loop code is updating the same object and placing a reference to that same, single object in each array element. In that structure, you really need a "new" VendorUpdateClass for each iteration.David W

1 Answers

3
votes

Your Javascript object should look something like this:

var vendorUpdatLists= [ { job: "ABC-123", task: "XYZ" },  { job: "ABC-333", task: "LAX" }];

And your method definition should look like:

public List<VendorUpdateClass> updateVendors(List<VendorUpdateClass> vendorUpdateItems) {
  // It's time to use the list!
  if (vendorUpdateItems != null && vendorUpdateItems.Count() > 0) {
    string job = vendorUpdateItems[0].job; // ABC-123
    string task = vendorUpdateItems[0].task; // XYZ
  }
}

Lastly, I recommend that you follow the proper naming conventions. For example, a public property should start with a capital letter.

string job = vendorUpdateItem.Job;