2012-08-26 4 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 당신은 JSON 형식으로 귀하의 결과를 반환 할 수 있습니다

[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의 답 중 하나와 결합하면 정말 잘 작동합니다. http://stackoverflow.com/questions/5088450/simple-mvc3-question-how-to-retreive-form-values-from-httppost-dictionary-oror –

0

. 확실하게 asp.net을 사용하여 이것을 수행하는 방법을 잘 모르지만 레일스라면 return @foo.to_json