Created
May 23, 2012 13:56
-
-
Save felipeleusin/2775361 to your computer and use it in GitHub Desktop.
ActionFilter para converter para Ajax a Action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReturnPartialIfAjaxAttribute : ActionFilterAttribute | |
{ | |
private readonly string partialViewName; | |
public ReturnPartialIfAjaxAttribute(string partialViewName) | |
{ | |
this.partialViewName = partialViewName; | |
} | |
public override void OnActionExecuted(ActionExecutedContext filterContext) | |
{ | |
if (filterContext.Exception != null || filterContext.Canceled == true) | |
{ | |
return; | |
} | |
var viewResult = filterContext.Result as ViewResult; | |
if (viewResult != null && filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) | |
{ | |
var partialResult = new PartialViewResult(); | |
partialResult.ViewName = partialViewName; | |
partialResult.ViewData.Model = viewResult.Model; | |
filterContext.Result = partialResult; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment