/*************************ActionMethodController.cs************************************/
using System;
using System.Web.Mvc;
using Chapter15.Areas.ActionMethodSelector.Utility;
namespace Chapter15.Areas.ActionMethodSelector.Controllers
{
public class ActionMethodController : Controller
{
public ActionResult Index()
{
return View();
}
[AjaxOnly]
public ActionResult Index(FormCollection unused)
{
return Content(String.Format("<p>" + DateTime.Now + "</p>"));
}
}
}
/************************************AjaxOnlyAttribute .cs********************************************/
using System.Reflection;
using System.Web.Mvc;
namespace Chapter15.Areas.ActionMethodSelector.Utility
{
public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext,
MethodInfo methodInfo)
{
return controllerContext.HttpContext.Request.IsAjaxRequest();
}
}
}
/**************************************Index.cshtml***********************************************/
@{ ViewBag.Title = "Action Method Selector Sample"; }
<h3>Dates go here for each submit...</h3>
<div id="dates"></div>
@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "dates" }))
{
<input type="submit" />
}