2012-08-26 22 views
5

He visto cómo puedo serializar a un objeto en JSON. ¿Cómo puedo PUBLICAR una cadena que devuelve un ViewResult?Publicar una cadena de mvc

  $.ajax({ 
       url: url, 
       dataType: 'html', 
       data: $(this).val(), //$(this) is an html textarea 
       type: 'POST', 
       success: function (data) { 
        $("#report").html(data); 
       }, 
       error: function (data) { 
        $("#report").html('An Error occured. Invalid characters include \'<\'. Error: ' + data); 
       } 
      }); 

MVC

[HttpPost] 
    public ActionResult SomeReport(string input) 
    { 
     var model = new ReportBL(); 
     var report = model.Process(input); 
     return View(report); 
    } 
+0

Tener un vistazo a este post y la respuesta de Darin: http://stackoverflow.com/questions/5046930/jquery-send-string-as -post-parameters – Sam

Respuesta

5

¿Qué tal:

 $.ajax({ 
      url: url, 
      dataType: 'html', 
      data: {input: $(this).val()}, //$(this) is an html textarea 
      type: 'POST', 
      success: function (data) { 
       $("#report").html(data); 
      }, 
      error: function (data) { 
       $("#report").html('An Error occured. Invalid characters include \'<\'. Error: ' + data); 
      } 
     }); 

Si usted hace la data un objeto JSON con una clave que coincide con el nombre del parámetro MVC debe recogerlo.

En el lado MVC ...

[HttpPost] 
public ActionResult SomeReport() 
{ 
    string input = Request["input"]; 
    var model = new ReportBL(); 
    var report = model.Process(input); 
    return View(report); 
} 
+0

Combiné esto con una de las respuestas de Darin y funciona muy bien. http://stackoverflow.com/questions/5088450/simple-mvc3-question-how-to-retreive-form-values-from-httppost-dictionary-or –

0

Es posible que desee devolver el resultado como un formato JSON. No estoy seguro de cómo hacer esto exactamente con asp.net, pero si fuera Rails, sería return @foo.to_json

Cuestiones relacionadas