简体   繁体   English

无法使用 [FromBody] 获取键值对的值

[英]Can not get a value of key-value pair using [FromBody]

Data is sent from front-end to back-end.数据从前端发送到后端。 Request body is created like this请求正文是这样创建的

var stateWithValue = {};

for (const item in self.pricings()) {
    state[item] = self.pricings()[item]["Comment"];
}

var request = {
    generalComment: self.generalComment(),
    stateWithValue: stateWithValue
};

Request body looks like this请求体看起来像这样

{
generalComment: "test comment",
stateWithValue:
{
  Delaware: "Value1",
  California: "Value2",
  Texas: "Value3"
}
}

Number of elements in stateWithValue can be different for every request.每个请求的stateWithValue中的元素数量可能不同。

In the back-end, data is taken using [FromBody] attribute在后端,使用 [FromBody] 属性获取数据

public WebApiResponseGeneric<EmptyResponse> State([FromBody] StateRequest request)

StateRequest.cs状态请求.cs

public class StateRequest : PublicRequest
{
    public string GlobalComment { get; set; }
    public StateWithValue StateWithValue { get; set; }
}
public class StateWithValue
{
    public string State { get; set; }
    public string Value { get; set; }
}

In.network tab(dev console) payload looks like this In.network 选项卡(开发控制台)有效负载如下所示

generalComment: test general comment stateWithValue[Delaware]: Value1 stateWithValue[California]: Value2 stateWithValue[Texas]: Value3 generalComment: 测试一般评论 stateWithValue[Delaware]: Value1 stateWithValue[California]: Value2 stateWithValue[Texas]: Value3

The Problem In back-end request object, StateWithValue.State StateWithValue.Value are both null.问题后端请求object,StateWithValue.State StateWithValue.Value都是null。

YOu have to post PublicRequest class too.您也必须发布 PublicRequest class。 But for your request body json, your class should be但是对于您的请求正文 json,您的 class 应该是

public class StateRequest : PublicRequest
{
    public string generalComment{ get; set; }
    public Dictionary<string, string> stateWithValue { get; set; }
}

or change或改变

public class StateRequest : PublicRequest
{
    public string GlobalComment { get; set; }
    public List<StateWithValue> StateWithValue { get; set; }
}

and javascript和 javascript

var stateWithValue = [];

for (const item in self.pricings()) {
    var stateWithValueItem={};
    stateWithValueItem.State =item;
    stateWithValueItem.Value = self.pricings()[item]["Comment"];
   
 stateWithValue.push(stateWithValueItem);
    
}

In order to map the JSON:为了map JSON:

{
generalComment: "test comment",
stateWithValue:
{
  Delaware: "Value1",
  California: "Value2",
  Texas: "Value3"
}
}

You would need to use the following C# model您需要使用以下 C# model

public class StateRequest : PublicRequest
{
    public string GeneralComment { get; set; }
    public StateWithValue StateWithValue { get; set; }
}
public class StateWithValue
{
    public string Dalaware { get; set; }
    public string California { get; set; }
    public string Texas { get; set; }
}

If you want to map multiple states then consider using an array instead, something like:如果你想要 map 多个状态,那么考虑使用一个数组,比如:

{
generalComment: "test comment",
stateWithValue:
[
  State: "Delaware", Value: "Value1",
  State: "California", Value: "Value2",
  State: "Texas", Value: "Value3"
]
}

But your model would need to be updated to look something like this:但是您的 model 需要更新为如下所示:

public class StateRequest : PublicRequest
{
    public string GeneralComment { get; set; }
    public List<StateWithValue> StatesWithValues { get; set; }
}

Note: There is a typo with general vs Global in your sample code注意:您的示例代码中存在 general 与 Global 的拼写错误

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

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