tags:

views:

31

answers:

4

could a controllers action be used for both a regular web request and a ajax call?

For a regular web request, the action would take in a forms collection, associate the category to the article and then redirect to another page.

for a ajax request, it would associate the category to the article, but instead of redirecting it would send some sort of a response message back.

is this a good practice or should I just create 2 actions?

+1  A: 

Create two Actions which both use the same method for the business logic. Whoever maintains this will thank you.

Yuriy Faktorovich
A: 

I think it's better to separate the actions, and reuse as much code as possible using private methods in your controller.

I'm pretty sure it is possible to detect an ajax call, but it kinda hurts the single responsibility principle.

Sander Rijken
A: 

Its a good practice whenever write action for ajax be reponsible for none JS enabled users so every ajax action should control if the request is from ajax or a normal request to can handle both .

ali62b
+1  A: 

My prefered method for this is

public ActionResult UpdateJS(int id)
{
   var retVal = Update(id);
   return View("UpdateJS", retVal);    
}

public ActionResult UpdateReg(int id)
{
   var retVal = Update(id);
   return View("UpdateReg", retVal);
}

public object Update(int id)
{
   //Do something here
   return id;
}
Hurricanepkt