I want to know how to use Ajax in MVC2. I created an empty project in Visual Studio and added a home controller under Controllers/Home/HomeController.cs with the following code
How do I use AJAX to call the AjaxTest method on the HomeController when the button is clicked and display that text instead?
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult AjaxTest()
{
return Json("Whoever answers this rocks!");
}
}
I added a view under Views/Home/Index.cs with the following code
<script type="text/javascript">
function sayHello() {
alert("hello stackoverflow :)")
}
</script>
<div>
Hello
<button onclick="sayHello();"> Click Me! </button>
</div>