I am just exploring asp.net mvc as I want to switch from Webforms. I was just experimenting by trying to post a string using jQuery and getting that string back in the response. However, I am not sure how to access the the post parameter within the action method of the controller.
I tried using the FormCollection but it is empty (which I guess is obvious since I am posting using a jQuery ajax call and not a form)
$(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