执行Ajax时出现500内部服务器错误

时间:2014-03-19 07:32:26

标签: c# jquery ajax asp.net-mvc

我有一个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必须在其他地方。你能告诉我我做错了什么吗?

2 个答案:

答案 0 :(得分:3)

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

[AdminAuthorization]
[ValidateAntiForgeryToken]

将其删除并尝试一次。

答案 1 :(得分:0)

500内部服务器错误通常表示您调用的页面中存在致命错误(/ DeviceUsage / Edit)

您可以尝试使用页面中设置的变量手动运行页面,而不是发布它们。