Soy nuevo en MVC3, me gustaría saber si es posible devolver un valor a un diálogo modal. Vea el ejemplo a continuación.
En mi archivo .cshtml, cuando se hace clic en el botón "registro", se llama Acción de registro.
<button name="button" value="log" id="log">Log</button>
<div id="dialog-message" title="Input Error!">
<p>
<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
The result from the controller is :
</p>
</div>
En mi controlador Acción tengo este
public ActionResult Log(FormCollection collection)
{
if(x+2!=4)
{
// return the value of x to the modal dialog
}
else
{
// save record to database
}
}
Quiero que el diálogo modal de jquery muestre el valor de x.
En el script jquery, tengo el siguiente
$("#dialog-message").dialog({
autoOpen: false,
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
$("#log").click(function () {
$("#dialog-message").dialog("open");
this.defaultShowErrors();
});
¡¡¡Por favor ayuda!!!
Respuestas
2 para la respuesta № 1Parece que te faltan dos cosas: 1) un contenedor para el resultado, y 2) la llamada Ajax para obtener el resultado. Primero, el contenedor:
The result from the controller is : <span id="result_container"></span>
Y la llamada del Ajax:
$("#log").click(function() {
var ajaxUrl = // set the URL for **Log** here
$.get(ajaxUrl, function(data) {
$("#result_container").html(data);
});
$("#dialog-message").dialog("open");
});
En el controlador, acaba de devolver el resultado como texto simple:
if(x+2!=4)
{
// return the value of x to the modal dialog
return new ContentResult() {
Content = x,
ContentEncoding = System.Text.Encoding.UTF8,
ContentType = "text/plain"
};
}
0 para la respuesta № 2
Gracias a todos, lo tengo funcionando después de leer este post.