Angular 12 post to .Net core 6 Api all values are being received but int type is always null/0(zero) in the model

huangapple go评论71阅读模式
英文:

Angular 12 post to .Net core 6 Api all values are being received but int type is always null/0(zero) in the model

问题

当我从Angular 12应用程序传递一个模型时,所有属性都被正确分配并在API端接收,除了整数类型。ModelId 始终为null/零。

我的Angular服务代码如下:

const httpOptions = {
  headers: new HttpHeaders({    
    'Content-Type': 'application/json; charset=utf-8',
    'Accept': 'application/json'
  })
};

getComparables(comparableQuery: ComparableFilterModel): Observable<ResponseWrapper<SimpleCycleValuation>> {
  var parameter = JSON.stringify(comparableQuery);
  return this.http.post<ResponseWrapper<SimpleCycleValuation>>(
                   `${environment.baseUrl}GetSimpleCylComparables`, parameter, httpOptions);
}

我的API代码如下:

[HttpPost(template: ApiRoutes.SimpleCycle.GetSimpleCylComparables)]
public async Task<ActionResult> GetSimpleCylComparables([FromBody] GetComparableQuery comparableQuery)
{
     //Code
}

我的模型代码如下:

export class ComparableFilterModel
 {
    baseModelId: number;
    excludedComparableIds: number[];
    valueType: string;
}

public class GetComparableQuery
{
    /// <summary>
    /// Gets or sets the value of SCModelId.
    /// </summary>
    public int BaseModelId;

    /// <summary>
    /// Gets or sets the ExcludedComparableIds.
    /// </summary>
    public List<int> ExcludedComparableIds { get; set; }

    /// <summary>
    /// Gets or sets the ValueType.
    /// </summary>
    public string ValueType { get; set; }
}

截图 -

Angular 12 post to .Net core 6 Api all values are being received but int type is always null/0(zero) in the model

Angular 12 post to .Net core 6 Api all values are being received but int type is always null/0(zero) in the model

英文:

When I pass a model from Angular 12 application, all properties are properly assigned and received at api side except the integer type. ModelId is always null/zero.

My angular service -

const httpOptions = {
  headers: new HttpHeaders({    
    &#39;Content-Type&#39;: &#39;application/json; charset=utf-8&#39;,
     &#39;Accept&#39;: &#39;application/json&#39;
  })
};
    getComparables(comparableQuery: ComparableFilterModel): Observable&lt;ResponseWrapper&lt;SimpleCycleValuation&gt;&gt; {

  var parameter = JSON.stringify(comparableQuery);
  return this.http.post&lt;ResponseWrapper&lt;SimpleCycleValuation&gt;&gt;
                   (`${environment.baseUrl}GetSimpleCylComparables`, parameter,httpOptions);
  }

My API -

[HttpPost(template: ApiRoutes.SimpleCycle.GetSimpleCylComparables)]
    public async Task&lt;ActionResult&gt; GetSimpleCylComparables([FromBody] GetComparableQuery comparableQuery)
    {
             //Code
    }

My Model -

export class ComparableFilterModel
 {
    baseModelId:number;
    excludedComparableIds:number[];
    valueType:string;
}

public class GetComparableQuery
{
    /// &lt;summary&gt;
    /// Gets or sets the value of SCModelId.
    /// &lt;/summary&gt;
    public int BaseModelId;

    /// &lt;summary&gt;
    /// Gets or sets the ExcludedComparableIds.
    /// &lt;/summary&gt;
    public List&lt;int&gt; ExcludedComparableIds { get; set; }

    /// &lt;summary&gt;
    /// Gets or sets the ValueType.
    /// &lt;/summary&gt;
    public string ValueType { get; set; }
}

Screenshot -

Angular 12 post to .Net core 6 Api all values are being received but int type is always null/0(zero) in the model

Angular 12 post to .Net core 6 Api all values are being received but int type is always null/0(zero) in the model

答案1

得分: 0

抱歉,我忘记为BaseModelId添加getter和setter方法了...

英文:

Well that's embarrassing...I forgot to add getter and setter for BaseModelId.

huangapple
  • 本文由 发表于 2023年7月13日 15:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定