我有两个不同的模型需要传递给Web API。这两个样本模型如下
public class Authetication
{
public string appID { get; set; }
}
public class patientRequest
{
public string str1 { get; set; }
}
为了开始工作,我创建了如下的第三个模型。
public class patientMaster
{
patientRequest patientRequest;
Authetication Authetication;
}
并传递我根据以下jquery代码创建的数据
var patientMaster = {
patientRequest : { "str1" : "John" },
Authetication : { "appID" : "Rick" }
}
$.ajax({
url: "http://localhost:50112/api/Patient/PostTestNew",
type: "POST",
data: {"": patientMaster}
});
并抓住这个我在控制器中创建了以下方法
[HttpPost]
public string PostTestNew(patientMaster patientMaster)
{
return " .. con .. ";
}
我的问题是
每当测试时我得到patientMaster
对象但我没有得到任何数据Authetication
对象也没有patientRequest
对象
我也尝试在jquery中传递contenttype:json,但它不起作用
有人可以帮我吗?
你很近。我添加了FromBody属性并指定了内容类型。我还将使您patientMaster
对象中的属性可公开访问。
PatientMaster对象:
public class patientMaster
{
public patientRequest patientRequest { get; set;}
public Authetication Authetication { get; set;}
}
API控制器:
[HttpPost]
public string PostTestNew([FromBody]PatientMaster patientMaster)
{
return "Hello from API";
}
jQuery代码:
var patientRequest = { "str1": "John" };
var authentication = { "appID": "Rick" };
var patientMaster = {
"PatientRequest": patientRequest,
"Authentication": authentication
};
$.ajax({
url: "http://localhost:50112/api/Patient/PostTestNew",
type: "POST",
data: JSON.stringify(patientMaster),
dataType: "json",
contentType: "application/json",
traditional: true
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句