Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any attribute relating to AJAX to be set for ASP.NET MVC controller actions?

I want to use partial views with AJAX calls in ASP.NET MVC, and this is the first time I'm using it. I just searched to see if there is anything special I should know beforehand, and one of'em that I'm curious about, is to see if there is any special attribute that should be set or is related to AJAX calls? Something like [ChildActionOnly] or [HttpGet]

like image 602
Saeed Neamati Avatar asked Jul 02 '11 18:07

Saeed Neamati


People also ask

Can we use AJAX in MVC?

The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features. To enable the unobtrusive AJAX support in the MVC application, open the Web.

Can AJAX work with ASP NET?

AJAX is used to create dynamic web pages that do not require page reloading when any part of the whole web page content or the whole web page content is changed. The server data exchange is asynchronous in nature and AJAX in ASP.net uses multiple technologies like XSLT, XHTML, CSS, JavaScript, etc.


1 Answers

I don't think there is built in attribute for ajax, but you can create your own AjaxOnly filter like this:

public class AjaxOnlyAttribute : ActionMethodSelectorAttribute  {     public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)     {         return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();     } } 

And decorate your action methods like this:

[AjaxOnly] public ActionResult AjaxMethod() {     } 

See Also: ASP.NET MVC Action Filter – Ajax Only Attribute for another way of implementing this

like image 189
Muhammad Adeel Zahid Avatar answered Sep 24 '22 12:09

Muhammad Adeel Zahid