2012-08-26 19 views
5

JSON'ta bir nesneyi nasıl serileştirebileceğimi gördüm. Bir ViewResult döndüren bir dizeyi nasıl POST gönderebilirim?mvc'ye bir dize gönderin

  $.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 Bir json biçimi olarak sonuç döndürmez isteyebilirsiniz

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

bir bu yazı bakmak ve Darin yanıtını Var: http://stackoverflow.com/questions/5046930/jquery-send-string-as -post-parametreleri – Sam

cevap

5

ne dersiniz: Eğer MVC onu almak gerekir parametre adıyla eşleşen bir anahtarla data bir JSON nesnesi yaparsanız

 $.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); 
      } 
     }); 

. MVC tarafında

...

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

Bunu Darin yanıtlarından biriyle birleştirdim ve gerçekten iyi çalışıyor. http://stackoverflow.com/questions/5088450/simple-mvc3-question-how-to-retreive-form-values-from-httppost-dictionary-or –

0

. Bunu asp.net ile tam olarak nasıl yapacağınızdan emin değilsiniz, ancak Rails ise, return @foo.to_json