我已经了解了如何序列化为 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);
}
最佳答案
怎么样:
$.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);
}
});
如果您将 data
设为具有与参数名称匹配的键的 JSON 对象,MVC 应该将其拾取。
在 MVC 方面...
[HttpPost]
public ActionResult SomeReport()
{
string input = Request["input"];
var model = new ReportBL();
var report = model.Process(input);
return View(report);
}
关于c# - 将字符串发布到 mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12132537/