Click here to Skip to main content
15,881,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm having a tough time getting the result from my controller, it has a function that contains a delegate funciont passint as a parameter.

so the result is awais empty and then goes back after its finished.

HTLM
HTML
<label>Name:</label>
<input id="txtName" type="text" />
<label>lastName:</label>
<input id="txtlastName" type="text" />

<pre lang="HTML"></pre>&lt;button onclick="SayHello()">HELLO</button>


JS

JavaScript
function SayHello() {
        alert(&quot;In&#237;cio&quot;);
        var user = CaptureElements();
        var json = JSON.stringify(user);        
        var url = '@Url.Action("SayHello&", "Home")'; 

$.ajax({
    url: url,
    async: false,
    data: json,
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    success: function (data) {
                    $("#aaa")[0].innerHTML = data;                    
                    alert(data);
                }
});    


function CaptureElements() {        
        var name = document.getElementById("txtName").value;
        var lastname = document.getElementById("txtlastName").value;        
        return (name == "" ? null : { name: name, lastname: lastname })

    }



C#

C#
 [HttpPost]
 public ActionResult SayHello(Usuario user)
 {
   var Proxy = content();
   string hello= "";
   Proxy.SayHello(user.name.ToString(), user.lastName.ToString(),
                    (String result) =>
                    {
                       hello= result;

                    }, ExceptionCallback);

  return Json(hello);
}


CSharp / DataSnap
The "Proxy" class below was generated by delphi xe5 data snap project.


C#
/**
       * @param FirstName [in] - Type on server: string
       * @param LastName [in] - Type on server: string
       * @return result - Type on server: string
       */
      public delegate void SayHelloCallback(String Result);

      public void SayHello(String FirstName, String LastName, SayHelloCallback callback = null, ExceptionCallback ExCal = null)
      {
        DSRESTCommand cmd = getConnection().CreateCommand();
        cmd.setRequestType(DSHTTPRequestType.GET);
        cmd.setText("TServerMethods1.SayHello");
        cmd.prepare(get_TServerMethods1_SayHello_Metadata());
        InternalConnectionDelegate SayHelloDel = () =>
        {
          if (callback != null)
          {
            try
            {
              callback.DynamicInvoke(cmd.getParameter(2).getValue().GetAsString());
            }
            catch (Exception ex)
            {
              if (ExCal != null) getConnection().syncContext.Send(new SendOrPostCallback(x => ExCal.DynamicInvoke(ex.InnerException)), null);
              else getConnection().syncContext.Send(new SendOrPostCallback(x => BaseExCal.DynamicInvoke(ex.InnerException)), null);
            }
          }
        };
        cmd.getParameter(0).getValue().SetAsString(FirstName);
        cmd.getParameter(1).getValue().SetAsString(LastName);
        getConnection().execute(cmd, this, SayHelloDel, ExCal);
      }



I would really appreciate any help!
Thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900