szpic
szpic

Reputation: 4498

500 Internal server error when executing Ajax

I have a JS script:

$(document).ready(function () {
    $('.z').on('click', function (event) {
        event.preventDefault();
        $.ajax({
            url: "/DeviceUsage/Edit",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            headers: {
                'RequestVerificationToken': '@TokenHeaderValue()'
            },
            data: JSON.stringify({
                deviceusage: {
                    DeviceInstanceId: $('.a').children("#DeviceInstanceId").val(),
                    UserId: $('.a').children('#UserId').val(),
                    storageId: $('.a').children('#storageId').val()

                }
            }),
            error: function (data) {
                alert("wystąpił nieokreślony błąd " + data);
            },
            success: function (data) {
                alert(data.newurl);
                if (data.ok) {
                    $("#Modal").modal('hide');
                    window.location = data.newurl;
                }
                else {
                    $('.modal-body').html(data);
                }
            }
        })
    })
@functions{
    public string TokenHeaderValue()
    {
        string cookieToken, formToken;
        AntiForgery.GetTokens(null, out cookieToken, out formToken);
        return cookieToken + ":" + formToken;
    }
}
});

And a method In DeviceUsage controller:

[HttpPost]
    [AdminAuthorization]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include="StorageId,UserId,DeviceInstanceId")] DeviceUsage deviceusage)
    {
        if (deviceusage.UserId == 6 && deviceusage.StorageId==3)
        {
            ModelState.AddModelError("", "Zarezerwowane urządzenie nie moze byc przypisane do biurka");
        }
        if(deviceusage.UserId==1 && deviceusage.StorageId==3)
        {
            ModelState.AddModelError("", "Wolne urządzenie nie może przebywać na jakimś biurku");
        }
        if((deviceusage.UserId!=1 & deviceusage.UserId!=6)&deviceusage.StorageId!=3)
        {
            ModelState.AddModelError("", "Urzązenie przypisane do kogos nie moze przebywac w magazynie");
        }
        if (ModelState.IsValid)
        {

            unitOfWork.deviceUsageRepository.Update(deviceusage);
            unitOfWork.Save();
            return Json(new { ok = true, newurl = Url.Action("Index") });
        }
        ViewBag.DeviceInstanceId = new SelectList(unitOfWork.deviceInstanceRepository.Get(), "Id", "SerialNo", deviceusage.DeviceInstanceId);
        ViewBag.StorageId = new SelectList(unitOfWork.storageRepository.Get(), "Id", "Name", deviceusage.StorageId);
        var data = unitOfWork.userRepository.Get()
        .Select(s => new
        {
            Id = s.Id,
            Credentials = s.Name + " " + s.Surname
        }
        );
        ViewBag.UserId = new SelectList(data, "Id", "Credentials", deviceusage.UserId);
        return PartialView(deviceusage);
    }

I was trying to put a breakpoint at the start of the C# method but it was never hit so Error must be somewhere else. Can you tell me what I'm doing wrong?

Upvotes: 1

Views: 401

Answers (2)

Ashwini Verma
Ashwini Verma

Reputation: 7525

I guess any of these two attribute restricting to access the function.

[AdminAuthorization]
[ValidateAntiForgeryToken]

remove it and try once.

Upvotes: 3

Sean Routledge
Sean Routledge

Reputation: 1228

500 Internal server error usually means there was a fatal error in the page you called (/DeviceUsage/Edit)

You can try manually running the page with the variables set in the page instead of posting them.

Upvotes: 0

Related Questions