【发布时间】:2012-02-25 07:49:00
【问题描述】:
我只是在探索 asp.net mvc,因为我想从 Webforms 切换。我只是在尝试使用 jQuery 发布一个字符串并将该字符串返回到响应中。但是,我不确定如何在控制器的 action 方法中访问 post 参数。
我尝试使用 FormCollection,但它是空的(我想这很明显,因为我使用 jQuery ajax 调用而不是表单发布)
$(function () {
$("#GetReport").click(function () {
$.ajax({
type: 'POST',
url: '/Reports/GetReport',
data: 'Abracadabra Mercedes',
contentType: 'application/text;charset=utf-8',
dataType: 'text',
success: function (result) {
alert(result);
}
});
});
});
//Controller Code
public class ReportsController : Controller
{
//
[HttpPost]
public ActionResult GetReport(string query)
{
ViewBag.Result = "Hello";
ViewBag.Geronimo = query;
return View();
}
}
//View Code
@{
Layout = null;
}
@ViewBag.Result + @ViewBag.Geronimo
【问题讨论】:
标签: jquery asp.net asp.net-mvc asp.net-mvc-3