2012-08-26 18 views
5

पर एक स्ट्रिंग पोस्ट करें मैंने देखा है कि मैं JSON में किसी ऑब्जेक्ट को क्रमबद्ध कैसे कर सकता हूं। मैं एक स्ट्रिंग कैसे पोस्ट कर सकता हूं जो ViewResult देता है?एमवीसी

  $.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 -पोस्ट-पैरामीटर – Sam

उत्तर

5

कैसे के बारे में:

 $.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 यह उठाना चाहिए से मेल खाता है साथ data एक JSON ऑब्जेक्ट करते हैं।

MVC तरफ ...

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

मैंने इसे डारिन के उत्तरों में से एक के साथ जोड़ा और यह वास्तव में अच्छी तरह से काम करता है। http://stackoverflow.com/questions/5088450/simple-mvc3-question-how-to-retreive-form-values-from-httppost-dictionary-or –

0

आप एक json प्रारूप के रूप में अपने परिणाम वापस जाने के लिए चाहते हो सकता है। यह सुनिश्चित नहीं है कि एएसपीनेट के साथ इसे वास्तव में कैसे किया जाए, लेकिन यदि यह रेल थे, तो यह return @foo.to_json