Je rencontre des difficultés avec le problème suivant, si quelqu'un a une idée de comment le faire fonctionner, je l'apprécierais vraiment.Comment renvoyer une valeur à partir d'un délégué de rappel à partir de actionResult mvc4
Comme je m appelle un ActionResult d'une fonction jquery, qui a une fonction de rappel
comme vous pouvez le voir ici.
function SayHello() {
var user = CaptureElements();
var json = JSON.stringify(usuario);
var url = '@Url.Action("SayHello", "User")';
$("#divLoading").show();
$.ajax(
{
url: url,
type: 'POST',
dataType: 'json',
data: json,
contentType: 'application/json; charset=utf-8',
success: function (data) {
$("#aaa")[0].innerHTML = data;
$("#divLoading").hide();
}
});
}
function CaptureElements() {
var FisrtName = document.getElementById("txtNome").value;
var LastName = document.getElementById("txtLastName").value;
return (name == "" ? null : { name: name, lastName: lastName,})
}
ceci est mon ActionResult.
[HttpPost]
public ActionResult SayHello(User usr)
{
var hello = "";
var Proxy = conection();
Proxy.SayHello(usr.FisrtName.ToString(), usr.LastName.ToString(),
(String Result) =>
{
hello = Result;
}, ExceptionCallback);
return Json(hello, JsonRequestBehavior.AllowGet);
}
public void ExceptionCallback(Exception e)
{
//...
}
Le problème est donc, être un rappel fonction « bonjour » renvoie d'abord nulle puis il va à la fonction réelle,
Ce code (DSProxy.cs classe) a été créé à partir d'un serveur Delphi xe5 on data snap rest Mobile à C# silverligth, et im essayant de l'utiliser avec mvc4.
/**
* @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);
}
merci à l'avance
J'étais succesfull à l'aide WindowsForm avec une boîte de message, même peuplé d'une grille et d'autres choses.
private void button1_Click(object sender, EventArgs e)
{
var Proxy = conexao();
Proxy.SayHello(txtName.Text, txtLastName.Text,(String Result) =>
{
MessageBox.Show(Result);
}, ExceptionCallback);
}
Vous devriez envisager de configurer une socket Web afin d'utiliser cette approche de rappel. signalr est une intégration très facile dans mvc –
merci! Je vais jeter un coup d'oeil à elle. – Marisco
j'ai réussi à utiliser windowsform ... – Marisco