skip to Main Content

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.

In the back-end, data is taken using [FromBody] attribute

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

StateRequest.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

generalComment: test general comment
stateWithValue[Delaware]: Value1
stateWithValue[California]: Value2
stateWithValue[Texas]: Value3

The Problem
In back-end request object, StateWithValue.State StateWithValue.Value are both null.

2

Answers


  1. YOu have to post PublicRequest class too. But for your request body json, your class should be

    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

    var stateWithValue = [];
    
    for (const item in self.pricings()) {
        var stateWithValueItem={};
        stateWithValueItem.State =item;
        stateWithValueItem.Value = self.pricings()[item]["Comment"];
       
     stateWithValue.push(stateWithValueItem);
        
    }
    
    Login or Signup to reply.
  2. In order to map the JSON:

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

    You would need to use the following 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:

    {
    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:

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search