Skip to content

Instantly share code, notes, and snippets.

@LukeWinikates
Created July 23, 2011 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukeWinikates/1100848 to your computer and use it in GitHub Desktop.
Save LukeWinikates/1100848 to your computer and use it in GitHub Desktop.
A possibly naive way to write less code when you want a Controller POST action to play nicely with both vanilla and Ajax form submits.
public static class ControllerHelper {
// working with forms on MVC 3, I found a handful of instances where I wanted to support ajax form submits using the jQuery form plugin
// but still wanted to keep the option of graceful degradation if javascript wasn't supported.
// there might be a better approach to this, like having seperate Ajax and vanilla Post methods,
// moving the logic that currently resides in the controller elsewhere. That would increase complexity a lot all at once.
// Perhaps this kind of thing is the right path in the short term.
public static ActionResult PartialIfAjax_ViewOtherwise(this Controller controller, string viewName, object model) {
if (controller.Request.IsAjaxRequest()) {
return controller.PartialView(viewName, model);
}
return controller.View(viewName: viewName, model: model);
}
}
@kiquenet
Copy link

Works for MVC 5 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment