500执行Ajax时发生内部服务器错误

500执行Ajax时发生内部服务器错误

我有一个JS脚本:

$(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;
    }
}
});


以及DeviceUsage控制器中的一种方法:

[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);
    }


我试图在C#方法的开始处设置一个断点,但是它从未被击中,因此Error必须在其他地方。你能告诉我我做错了吗?

最佳答案

我猜这两个属性中的任何一个都限制了对该函数的访问。

[AdminAuthorization]
[ValidateAntiForgeryToken]


删除并尝试一次。

关于c# - 500执行Ajax时发生内部服务器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22499112/

10-09 04:46