c# - Passing POST parameter to WEB API2 -
i have 2 different model need passed web api. 2 sample model follow
public class authetication { public string appid { get; set; } } public class patientrequest { public string str1 { get; set; } }
so work have created 3rd model below.
public class patientmaster { patientrequest patientrequest; authetication authetication; }
and pass data have created following jquery code
var patientmaster = { patientrequest : { "str1" : "john" }, authetication : { "appid" : "rick" } } $.ajax({ url: "http://localhost:50112/api/patient/posttestnew", type: "post", data: {"": patientmaster} });
and catch have created following method in controller
[httppost] public string posttestnew(patientmaster patientmaster) { return " .. con .. "; }
my problem is
whenever testing getting patientmaster
object not getting data authetication
object nor patientrequest
object
i tried pass contenttype:json in jquery not work
can 1 me on this?
you pretty close. added frombody attribute , specified content type. i'd make properties in patientmaster
object publicly accessible.
patientmaster object:
public class patientmaster { public patientrequest patientrequest { get; set;} public authetication authetication { get; set;} }
api controller:
[httppost] public string posttestnew([frombody]patientmaster patientmaster) { return "hello api"; }
jquery code:
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 });
Comments
Post a Comment