"请求的资源不支持HTTP方法' PUT' ASP.Net Web API

时间:2016-12-08 20:31:25

标签: c# jquery asp.net-web-api kendo-treeview

我的jQuery Put请求不起作用,当我在调试模式下运行我的代码时,put函数没有被调用,而且从FireBug我可以看到这个错误:

  

请求的资源不支持HTTP方法' PUT'

这是我的jQuery代码和API控制器:

<script>
$(document).ready(function() {
    $('#success').hide();

    function onSelect(e) {
        $("#Type").prop("checked", false);

        var name = $("#TreeView").data("kendoTreeView").dataItem(e.node).Name;
        var accountId =$("#TreeView").data("kendoTreeView").dataItem(e.node).AccountId;

        $("#AccountID").val(accountId).prop("disabled",true);

        $("#Name").val(name).prop("disabled",true);

        var type = $("#TreeView").data("kendoTreeView").dataItem(e.node).Nature;

        $("#NatureAccountId").data("kendoDropDownList").value(type);

        $("#AccountTypeId").data("kendoDropDownList").value(type);
        $("#AccountTypeId").data("kendoDropDownList").enable(false);

        $("#BaseCurrencyId").data("kendoDropDownList").value(type);

        $("#ContraryToNatureCtrlId").data("kendoDropDownList").value(type);

        $("#BudgetCtrlTypeId").data("kendoDropDownList").value(type);

        var descrip = $("#TreeView").data("kendoTreeView").dataItem(e.node).Description;
        $("#Description").val(descrip);

        var idd = $("#TreeView").data("kendoTreeView").dataItem(e.node).ParentID;
        $("#ParentID").val(idd);

        var pName = $("#TreeView").data("kendoTreeView").dataItem(e.node).ParentName;
        $("#ParentName").val(pName).prop("disabled", true);

        var isActive = $("#TreeView").data("kendoTreeView").dataItem(e.node).IsActive;
        $("#IsActive").val(isActive);

        $("input:radio[name=Type][value='" + type + "']").prop("checked", true);
        $("input[name=Type]").prop("disabled", true);
    }

    var accountchild = {
        type: "odata",
        transport: {
            read: {
                url: function(options) {
                    return kendo.format("/api/Account/GetAccountChilds?ParentID={0}&OrganizationID={1}", options.AccountId, @Session["OrganizationID"].ToString());
                }

            }
        },
        schema: {
            model: {
                id: "AccountId",
                hasChildren: "HasChildren",
                type: "Type",
                name: "Name"

            }
        }
    };

    $("#TreeView").kendoTreeView({
        dataSource: new kendo.data.HierarchicalDataSource({
            type: "odata",
            transport: {
                read: {
                    url: "/api/Account/GetAllAccountParrentInfos?OrganizationID=@Session["OrganizationID"].ToString()",
                    dataType: "json"
                }
            },
            schema: {
                data: function(data) {
                    return data;
                },
                total: function(data) {
                    return data['odata.count'];
                },
                model: {
                    id: "AccountId",
                    hasChildren: "HasChildren",
                    children: accountchild,
                    type: "Nature",
                    name: "Name",
                    Description:"Description",
                    isActive:"IsActive",
                    parentName:"ParentName",
                    ParentID:"ParentID"
                }
            }
        }),
        select: onSelect,
        dataTextField: ["DisplayName", "DisplayName"],
        dataValueField: "AccountId"
    });
});
</script>

<script>
$("#AccountTypeId").kendoDropDownList({
    dataTextField: "Type",
    dataValueField: "Id",
    optionLabel: "انتخاب نوع حساب",
    dataSource: {
        transport: {
            read: {
                dataType: "json",
                url: "/api/Account/GetAllAccounttypes"
            }
        }
    }
});

$("#BaseCurrencyId").kendoDropDownList({
    dataTextField: "Currency",
    dataValueField: "Id",
    optionLabel: "انتخاب ارز پایه",
    dataSource: {
        transport: {
            read: {
                dataType: "json",
                url: "/api/Account/GetBaseCurrencies"
            }
        }
    }
});

$("#NatureAccountId").kendoDropDownList({
    dataTextField: "Nature",
    dataValueField: "Id",
    optionLabel: "انتخاب ماهیت حساب",
    dataSource: {
        transport: {
            read: {
                dataType: "json",
                url: "/api/Account/GetNatureAccounts"
            }
        }
    }
});

$("#ContraryToNatureCtrlId").kendoDropDownList({
    dataTextField: "Ctrl",
    dataValueField: "Id",
    optionLabel: "کنترل خلاف ماهیت",
    dataSource: {
        transport: {
            read: {
                dataType: "json",
                url: "/api/Account/GetContraryToNatureCtrls"
            }
        }
    }
});

$("#BudgetCtrlTypeId").kendoDropDownList({
    dataTextField: "CtrlType",
    dataValueField: "Id",
    optionLabel: "نوع کنترل بودجه",
    dataSource: {
        transport: {
            read: {
                dataType: "json",
                url: "/api/Account/GetBudgetCtrlTypes"
            }
        }
    }
});
</script>

<script>
$("#btn_edit").click(function() {
    var account = {
            "ParentID": $("#ParentID").val(),
            "AccountID": $("#AccountID").val(),
            "Name": $("#Name").val(),
            "Description": $("#Description").val(),
            "OrganizationID": $("#OrganizationID").val(),
            "IsDeleted": false,
            "RemainObligor": null,
            "Editable": true,
            "HasChildren": false,
            //"Type":   $("input[name=Type]").prop("checked", true).val(),
            "IsActive": $("#IsActive").prop("checked"),
            "BaseCurrencyId": $("#BaseCurrencyId").val(),
            "AccountTypeId": $("#AccountTypeId").val(),
            "BudgetCtrlTypeId": $("#BudgetCtrlTypeId").val(),
            "ContraryToNatureCtrlId": $("#ContraryToNatureCtrlId").val(),
            "NatureAccountId": $("#NatureAccountId").val()

        };

        $.ajax({
            type: "PUT",
            url: '/api/Account/Editt',
            data: JSON.stringify(account),
            contentType: "application/json;charset=utf-8",
            processData: true,
            success: function(data) {
                alert(`'موفق'`);
            },
            error: function() {
                alert('ناموفق');

            }
        });
    });
</script>


[AcceptVerbs("POST", "Get")]
[System.Web.Http.HttpPost]
public void Edittt([FromBody]AccountGridViewModel a)
{
    Account account=new Account();
    account.AccountID = a.AccountID;
    account.AccountTypeId = a.AccountTypeId;
    account.Name = a.Name;
    account.ParentID = a.ParentID;
    account.NatureAccountId = a.NatureAccountId;
    account.ContraryToNatureCtrlId = a.ContraryToNatureCtrlId;
    account.BaseCurrencyId = a.BaseCurrencyId;
    account.BudgetCtrlTypeId = a.BaseCurrencyId;
    account.Description = a.Description;
    account.IsActive = a.IsActive;
    account.Organization = a.Organization;
    account.OrganizationID = a.OrganizationID;
    _accountService.UpdateAccount(account);
}

1 个答案:

答案 0 :(得分:2)

删除

[AcceptVerbs("POST", "Get")]

并更改

[System.Web.Http.HttpPost]

[System.Web.Http.HttpPut]

HttpPost是AcceptVerbs的简写(&#34; POST&#34;)。你在哪里告诉控制器接受PUT。我不认为你可以有一个单一的控制器方法接受多个动词,即使你能让我感觉更干净的实现是两个同名的方法,一个用HttpGet装饰,一个用HttpPut装饰。