al.koval
al.koval

Reputation: 2542

ASP.NET Core post data not serealize custom field

I am using ASP.NET Core 3.1 and angular.

I send a post request from the object model to the controller method.

enter image description here

The controller method accepts an object at the input, but the bindingParameters field is empty.

enter image description here

The bindingParameters field is a list of KeyValueItem objects.

TmObject.cs

namespace v1.Atm
{
    public class TmObject
    {
        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("binding")]
        public string Binding { get; set; }

        [JsonProperty("sourceBindingParameters")]
        public string SourceBindingParameters { get; set; }

        [NotMapped]
        [JsonProperty("bindingParameters")]
        public List<TmKeyValueItem> BindingParameters
        {
            get
            {
                return JsonConvert.DeserializeObject<List<TmKeyValueItem>>(string.IsNullOrEmpty(SourceBindingParameters) ? "" : SourceBindingParameters);
            }
            set
            {
                SourceBindingParameters = JsonConvert.SerializeObject(value);
            }
        }

        [JsonProperty("caption")]
        public string Caption { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonIgnore]
        public string SourceParameterGroups { get; set; }

        [NotMapped]
        [JsonProperty("parameterGroups")]
        public List<string> ParameterGroups
        {
            get
            {
                return JsonConvert.DeserializeObject<List<string>>(string.IsNullOrEmpty(SourceParameterGroups) ? "" : SourceParameterGroups);
            }
            set
            {
                SourceParameterGroups = JsonConvert.SerializeObject(value);
            }
        }

        [NotMapped]
        [JsonProperty("parameters")]
        public List<TmObjectParameter> Parameters;

        [NotMapped]
        [JsonProperty("removeParameters")]
        public List<int> RemoveParameters { get; set; }

        public TmObject()
        {
            Parameters = new List<TmObjectParameter>();
            RemoveParameters = new List<int>();
        }
    }
}

If the bindingParameters field is changed, it works:

[NotMapped]
[JsonProperty("bindingParameters")]
public List<BindingParameter> BindingParameters{ get; set; }

Tell me, please, what could be the problem?

P.S. Prior to this, the project was implemented on ASP.NET Webforms and there the code described above worked.

update

I got out of the problem as follows. Opened the SourceBindingParameters field for visibility on the client by adding [JsonProperty ("sourceBindingParameters")]. And before sending data to the server, I serialize the values from BindingParameters to sourceBindingParameters.

  public updateTmObject(tmObject: TmObject) {
    tmObject.sourceBindingParameters = JSON.stringify(tmObject.bindingParameters);  

    return this.httpService.post('/v1/Editor/UpdateObject', JSON.stringify(tmObject.bindingParameters), this.httpOptions).subscribe(
      (response: any) => {
        this.reset();
        this.getTmObjects();
        return true;
      },
      error => {
        console.error("TmObjects|TmObjectsService.updateTmObject(): " + error.status);
      }
    );
  }

Upvotes: 0

Views: 66

Answers (1)

Alexander
Alexander

Reputation: 9632

The bindingParameters from request is not a list of string values but complex objects. You'd better create a class for this objects

public class BindingParameter
{
    public int Id { get; set; }

    public string Key { get; set; }

    public string Value { get; set; }

    public bool Visible { get; set; }
}

and declare the property as the following

public List<BindingParameter> BindingParameters{ get; set; }

Upvotes: 1

Related Questions