-1
var assetimage_id = $(this).closest(".assetImageWrapper").attr("data-assetimage_id");

    var dataToSend = JSON.stringify({ "Asset_ID": assetimage_id, "Description": $(this).val() });
        $.ajax({
            url: "/api/Assets/UpdateDescription",
            type: "PUT",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: dataToSend,
            success: function (data) {
                alert("success");
            }
    });

这是它应该达到的方法。

 [HttpPut]
        public Asset UpdateDescription(int Asset_ID, string Description)
        {
            return new AssetsService().UpdateAssetDescription(Asset_ID, Description);
        }

什么看起来不正常?该方法设置在名为 Assets 的 Web API 控制器中。所有其他方法都可以正常工作(GETS,POSTS)。这是我在 Visual Studio 2012 中按 F5 运行它的时候,所以没有更改 IIS 配置。Api 路由是默认路由。

我的 web.config 支持所有动词:

4

1 回答 1

0

默认情况下,像上面的“Asset_ID”这样的简单类型以及字符串“Description”都是从 Uri 绑定的。在您的场景中,您似乎在正文中发送内容,因此您需要相应地更改您的 api 签名。顺便说一句,您的操作也不能有多个 FromBody 参数。

对于复杂模型,您需要创建一个视图模型来包含它们:

创建后:

public class AssetEstimatedValueUpdate
    {
        public int Asset_ID { get; set; }
        public string EstimatedValue { get; set; }
    }

然后你可以把它传入,一切正常。

[HttpPut]
        public Asset UpdateDescription(AssetDescriptionUpdate _AssetDescriptionUpdate)
        {
            return new AssetsService().UpdateAssetDescription(_AssetDescriptionUpdate.Asset_ID, _AssetDescriptionUpdate.Description);
        }
于 2012-11-21T20:22:28.377 回答