Angular 2 Code 请求URl:http://loacalhost:8800/MyController/SaveBookings
let data = {
occupationListStr: occupations,
rOccupationListStr: roccsStr,
};
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post('MyController/SaveBookings', JSON.stringify(data),options)
.then(res => {
return res.json()
})
.catch(this.handleError);
C#代码
控制器
问题: Request.QueryString值OccupListStr和rOccupationListStr为空
public ActionResult SaveBookings()
{
dynamic occupationListStr = Request.QueryString["occupationListStr"];
dynamic rOccupationListStr = Request.QueryString["rOccupationListStr"];
<....Do something.....>
return <return something>;
}
答案 0 :(得分:1)
[httpPost]
public IHttpActionResult Post([FromBody]Occupation objOccupation)
{
}
) $test
答案 1 :(得分:1)
在您的问题中,您在请求正文中以Json
(使用JSON.stringify(data)
)发送数据,但在您的操作中,您希望查询字符串中包含数据。
您应该将动作中的Json
解析为某个模型:
// you can use your own model (some class to parse Json to) instead of "dynamic"
[HttpPost]
public ActionResult SaveBookings([FromBody]dynamic data)
{
var occupationListStr = data.occupationListStr;
var rOccupationListStr = data.rOccupationListStr;
<....Do something.....>
return <return something>;
}
或强>
您应该在Angular 2中更改您的请求:
this.http.post('MyController/SaveBookings?occupationListStr=' + occupations + '&rOccupationListStr=' + roccsStr, null, options)
.then(res => {
return res.json()
})
.catch(this.handleError);