2012-08-26 14 views
5

JSONでオブジェクトにシリアル化する方法を見てきました。 ViewResultを返す文字列をPOSTするにはどうすればよいですか?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

はこの記事を見て、ダーリンの答えを持っている: http://stackoverflow.com/questions/5046930/jquery-send-string-as -post-parameters – Sam

答えて

5

方法について:あなたはMVCはそれを拾う必要があるパラメータの名前と一致するキーでdata JSONオブジェクトを作成する場合は

 $.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側で

...

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

私はこれをDarinの答えの1つと組み合わせました。本当にうまく動作します。 http://stackoverflow.com/questions/5088450/simple-mvc3-question-how-to-retreive-form-values-from-httppost-dictionary-oror –

0

あなたはJSON形式として、あなたの結果を返すようにしたいかもしれません。 asp.netでこれを正確に行う方法がわかりませんが、それがRailsの場合はreturn @foo.to_json

関連する問題