【发布时间】:2019-01-13 04:36:05
【问题描述】:
我想将数据从 angular post 传递到 web api。但在 web api 中它显示为 null。
我的意图是通过 POST 请求将对象内部的列表传递给 Web Api。
Angular 服务 POST 方法:
postData(URL : string,body : any) : Observable<any> {
var dto = {sortcolumn:"CustomerName", sortorder:"asc", pagenumber:"0",pagesize: "10"
,ddlUnits: [{"Value": "20022", "Text": "Arvind"},{"Value": "20022", "Text": "Arvind"}]
};
let bodystr = JSON.stringify(dto);
const httpOptions = {
headers: new HttpHeaders({
//'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/json',
})
};
return this._http.post<any>(this.BaseURL + URL, bodystr,httpOptions)
// 当我将调用此服务方法时,我将在组件中订阅它。
}
WEB API 动作方法:
[Route("api/AllCustomers")]
[HttpPost]
public IHttpActionResult GetAllCustomerPOST([FromBody] CustomerDTO dto)
{
try
{
List<GetAllCustomer_Result> lst = clsDCustomer.GetAllCustomer().ToList();
return new CustomResponseResult(HttpStatusCode.OK, Request, lst);
}
catch
{
return new CustomResponseResult(HttpStatusCode.InternalServerError, Request, Constants.MSGInternamServerError);
}
}
CLASS 结构(在动作请求中传递)
public class CustomerDTO
{
public long ID { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Customer Name")]
public string CustomerName { get; set; }
[Required(ErrorMessage = "Required")]
[MaxLength(length: 10, ErrorMessage = "Maximun length for mobile no is 10 digit")]
[Display(Name = "Mobile No")]
public string MobileNo { get; set; }
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
[Display(Name = "Address")]
public string Address { get; set; }
public int CreatedBy { get; set; }
public System.DateTime CreatedOn { get; set; }
public List<DropDownList> ddlUnits { get; set; }
public int pagenumber { get; set; }
public int pagesize { get; set; }
public string sortcolumn { get; set; }
public string sortorder { get; set; }
}
public class DropDownList
{
public string Value { get; set; }
public string Text { get; set; }
}
我在 HttpParams() 中传递了带有 'Content-Type':'application/x-www-form-urlencoded' 标头的正文数据,并且可以正常工作,但无法在 WEB API 中获取 ddlUnits 属性。
我用谷歌的 POST MAN 用原始的 Body json 进行了测试。
{"sortcolumn":"CustomerName", "sortorder":"asc", "pagenumber":"0", "pagesize": "10","ddlUnits":[{"Value": "20022", “文本”:“Arvind”},{“值”:“20022”,“文本”:“Arvind”}]}
效果很好
【问题讨论】:
-
您能提供更多信息吗?您尝试了什么,什么不起作用...
-
请检查问题现在我已经提到了所有细节。
-
您是否碰巧解决了这个问题?我在 Angular 6 上遇到了类似的问题
标签: angular