您需要使用操作方法選擇器來區分Ajax請求和非Ajax請求。因此,實現ActionMethodSelectorAttribute並使用該屬性修飾您的操作方法(true)。請參閱以下示例代碼。
[HttpGet]
[MyAjax(true)]
public ActionResult ContactUs()
{
if (Request.IsAjaxRequest())
{
return PartialView("_ContactUs");
}
return View();
}
//..
public class MyAjaxAttribute : ActionMethodSelectorAttribute
{
private readonly bool _ajax;
public AjaxAttribute(bool ajax)
{
_ajax = ajax;
}
// Determines whether the action method selection is valid for the specified controller context
public override bool IsValidForRequest(
ControllerContext controllerContext,
MethodInfo methodInfo)
{
return _ajax == controllerContext.HttpContext.Request.IsAjaxRequest();
}
}
什麼是您用來調用此操作的JavaScript代碼 – stevethethread 2012-07-20 09:02:42