I am trying to streamline the process of returning the data from my WebMethod layer to the client and represent the set of parameters in coming from the client in a Dictionary<string,string>
to do something like this:
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static override ResultObject<List<PatientInfo>> GetResults(Dictionary<string, string> query)
{
ResultObject<List<PatientInfo>> resultObject = null;
if (!query.ContainsKey("finValue"))
{
resultObject = new ResultObject<List<PatientInfo>>("Missing finValue parameter from the query");
}
string finValue = query["finValue"];
if(finValue == null)
{
resultObject = new ResultObject<List<PatientInfo>>("Missing finValue parameter value from the query");
}
var patientData = GetPatientsByFin(finValue);
resultObject = new ResultObject<List<PatientInfo>>(patientData);
return resultObject;
}
}
My question is: how do I pass and de-serialize the Dictionary parameter?
To pass a dictionary, you have to use a WebService.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class TestService : System.Web.Services.WebService
{
[WebMethod]
public String PostBack(Dictionary<string, string> values)
{
//You should have your values now...
return "Got it!";
}
}
Then when you want to call it, you can pass something like this. Not sure if you're using jQuery, but here's an example using jQuery's ajax method.
var valueObject = {};
valueObject['key1'] = "value1";
valueObject['secondKey'] = "secondValue";
valueObject['keyThree'] = "3rdValue";
$.ajax({
url: 'TestService.asmx/PostBack',
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ values: valueObject }),
success: function (data) {
alert(data);
},
error: function (jqXHR) {
console.log(jqXHR);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With