简体   繁体   English

asp.net 5 / mvc 6 /模型活页夹/角形

[英]asp.net 5 / mvc 6 / model binder / angular

I do not know because when I do a post by angular, objects are not populated, such as categories or status. 我不知道,因为当我按角度进行发布时,不会填充对象,例如类别或状态。 Just the product. 只是产品。 However, note that the Request.Form list, the information is there. 但是,请注意,Request.Form列表中包含该信息。 The binder is not performed correctly. 活页夹执行不正确。 What am I doing wrong? 我究竟做错了什么? Is it any web api configuration? 它是任何Web API配置吗? I've tried sending data via application/json, [frombody] ... I'm out of options. 我尝试通过application / json发送数据,[frombody] ...我没有选择。 Thanks in advance. 提前致谢。

 var product = { id: 1, name: "Name", categories: [ { id: 1, name: "name 1" }, { id: 2, name: "name 2" } ], status: { id: 1, name: "active" } }; var config: ng.IRequestConfig; config = { url: "", method: "POST", headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' } }; self.$http.post("api/produto", $.param(product), config) .success(function () { alert("OK"); }); 

    [HttpPost]
    public ProductInfo Post(ProductInfo item)
    {
        return item;
    }


model image 模特形象

request image 索取图片

try this: 尝试这个:

$http({
        url: "api/produto",
        method: "POST",
        data: product,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })
    .then(function(response) {
            // success
    }, 
    function(response) { // optional
            // failed
    });

或者,您可以使用快捷方式:

$http.post('api/produto', data).then(successCallback, errorCallback);

Here's what I would try 这是我会尝试的

var product = {
  id: 1,
  name: "Name",
  categories: [
    { id: 1, name: "name 1" },
    { id: 2, name: "name 2" }
  ],
  status: { id: 1, name: "active" }
};

var config: ng.IRequestConfig;
config = { url: "", method: "POST", headers: { 'Content-Type': 'application/json;charset=utf-8;' } };

self.$http.post("api/produto", product, config)
.success(function () {
  alert("OK");
});

What is different is that I send the actual JavaScript object and it will be serialized as JSON. 不同的是,我发送了实际的JavaScript对象,并将其序列化为JSON。 Web API will be able to deserialize the JSON into a pure C# object. Web API将能够将JSON反序列化为纯C#对象。

Trying to match key/value pair is normally just madness on complex objects. 试图匹配键/值对通常只是对复杂对象的疯狂。

There was no way to do it. 没有办法做到。

Either I create a custom model binder, or I'll have the new web api 6 mvc always use the frombody and force the ship by json. 我要么创建一个自定义模型活页夹,要么我将拥有新的Web API 6 mvc始终使用frombody并通过json强制运送。

At least that's what I did with my tests. 至少那是我对测试所做的。 It worked just sending json and using frombody . 它只是发送json并使用frombody

The documentation, really changed bind the web api 2 for this new integrated model to MVC. 该文档确实进行了更改,将用于此新集成模型的Web api 2绑定到MVC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM