AnonyMouse
AnonyMouse

Reputation: 18660

MVC jquery post 500 error

could someone please explain this to me:

I have an MVC method I want to post some date to in jquery that looks like this:

public bool UpdateOfficeUser(InvoiceLine invoiceLineUpdates)
        {
            var invoiceLine = _unitOfWork.InvoiceLineRepository.Get(invoiceLineUpdates.InvoiceLineId);

            ...

            return true;
        }

It's called when a dropdown of class dropdownofficeapprover is changed:

//Office approver changed
    $(".dropdownofficeapprover").change(function () {
        var invoiceLineId = $(this).attr("invoiceLineId");

        $(".dropdownofficeapprover[invoicelineid='" + invoiceLineId + "'] option:selected").each(function () {
            var invoiceLine = {
                OfficeUserId: 7
            };
            alert(invoiceLine.InvoiceLineId);
            alert(invoiceLine.OfficeUserId);

            $.post('/Invoice/UpdateOfficeUser', invoiceLine, function (data) {

                if (data == 'False') {

                    alert("You must have at least two different approvers");
                    location.replace(window.location.pathname);
                }
            });
        });
    });

Everything works fine up until the post occurs. Then I get a 500 error.

Upvotes: 0

Views: 1599

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

Then I get a 500 error.

Try stepping through your code inside the controller action and see if it throws an exception and for what reason. Or even better: use a javascript debugging tool such as FireBug to trace the actual AJAX request, see the request and the response from the server. This should give you indication on what has went wrong so that you got this 500 error.

Also you don't seem to ever be assigning the InvoiceLineId property of your invoiceLine object that you are sending to the server.

Also you seem to be sending multiple AJAX requests for each dropdownlist matching your criteria which obviously is very non-efficient. I would recommend you fetching all the necessary data into an object or in your case an array of objects and sending a single AJAX request to the server. You will of course have to modify your controller action to work with this new view model that you would be sending.

Upvotes: 2

Related Questions