如何使用ajax从委托使用MVC控制器获取结果 [英] how to get a result from MVC controller with delegate using ajax

查看:65
本文介绍了如何使用ajax从委托使用MVC控制器获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难从我的控制器获取结果,它有一个包含委托函数passint作为参数的函数。



所以结果它是空的,然后在完成后返回。



HTLM

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

<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





JS

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

下面的代理类是由delphi xe5数据快照项目生成的。







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


/**
       * @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.

推荐答案

.ajax({
url:url,
async: false
数据:json,
类型:' POST'
contentType:' application / json'
dataType: json'
成功: function (数据){
.ajax({ url: url, async: false, data: json, type: 'POST', contentType: 'application/json', dataType: 'json', success: function (data) {


#aaa)[< span class =code-digit> 0 ]。innerHTML = data;
alert(data);
}
});


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

}
("#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

下面的代理类是由delphi xe5数据快照项目生成的。







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


/**
       * @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.


这篇关于如何使用ajax从委托使用MVC控制器获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆