英文:
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; }
}
截图 -
英文:
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({
'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);
}
My API -
[HttpPost(template: ApiRoutes.SimpleCycle.GetSimpleCylComparables)]
public async Task<ActionResult> GetSimpleCylComparables([FromBody] GetComparableQuery comparableQuery)
{
//Code
}
My Model -
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; }
}
Screenshot -
答案1
得分: 0
抱歉,我忘记为BaseModelId添加getter和setter方法了...
英文:
Well that's embarrassing...I forgot to add getter and setter for BaseModelId.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论