locked
Passing parameters from frontend using webservice RRS feed

  • Question

  • User279786496 posted

    Hi,

        I have a java script file where we are getting selected Id's and these id's I want to pass to web service method where we are calling stored procedure (Sql server) these id's values should go as parameter to stored procedure and retrieve the data from procedure and bind in the drop down list.

    Here is the code

    Java script file
     
          var params = [];
            params.push(CreateNewParameter("IdList", GetConcatenateIdsToDownload(selectedId)))
            var dsConfig = GetDataSource(StudentSvcUrl + "GetStudentParameter", params);

    Interface

    Interface

            [OperationContract(Name = "GetStudentParameter")]         [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]          StdGenericList<StdDropdownListItem> GetStudentParameter(Dictionary<int, string> selectedId);

    Svc Method


    public StdGenericList<StdDropdownListItem> GetStudentParameter(Dictionary<string, string> selectedId)
    {

    What ever Id's we are selected that is coming from JS file, The issue is with svc method - I am not able to get it in dictionary (selectedId) it is showing as NULL when I debug it.

    Monday, August 21, 2017 12:45 PM

All replies

  • User475983607 posted

    BerrySmith

    What ever Id's we are selected that is coming from JS file, The issue is with svc method - I am not able to get it in dictionary (selectedId) it is showing as NULL when I debug it.

    This generally indicates the data is not properly formatted.  Unfortunately, there is not enough information in the POST to guess where the bug is located.

    Also the Interface does not match the implementation and should not compile.  That's a bit concerning because it means the problem description does not match the POSTed code. 

    Monday, August 21, 2017 2:24 PM
  • User1168443798 posted

    Hi BerrySmith,

    >> The issue is with svc method - I am not able to get it in dictionary (selectedId) it is showing as NULL when I debug it.

    Could you run your service? I found you define Interface with “Dictionary<int, string> selectedId”, but you implement it by “Dictionary<string, string> selectedId”. It will throw error.

    I make a test to achieve your requirement to pass Dictionary from JS to WCF Rest Service.

    1. Create a WCF Service

            public Dictionary<int, string> GetStudentParameter(Dictionary<int, string> selectedId)
            {
                Dictionary<int, string> d = new Dictionary<int, string>();
                d.Add(1,"D1");
                d.Add(2, "D2");
                return d;
            }

    2. Call Service from JS

            function SendDictionary() {
                console.log("Hello");
                var selectedId = [];
                selectedId.push({ Key: 1, Value: "S1" });
                selectedId.push({ Key: 2, Value: "S2" });
                $.ajax({
                    url: '/Service1.svc/GetStudentParameter',
                    data:'{"selectedId":'+  JSON.stringify(selectedId) +'}',
                    contentType: 'application/json',
                    type: 'POST',
                    dataType: 'json',
                    success: function (result) {
                        console.log(result)
                    }
                });
            }

    Best Regards,

    Edward

    Tuesday, August 22, 2017 6:47 AM