views:

522

answers:

2

Hello

I'm developing an ASP.Net mvc application which has a requirement to do some small, atomic actions without an entire page postback. The logical way of doing this is of course with an ajax call.

My question is - What would people suggest is the best way of achieving this?

As far as I am aware I can do it along the following lines:

  • WCF + ajax
  • Controller actions within the mvc application - Possibly returning as JSON result.
  • Webservices (seperate/combined project) + ajax

I have a requirement that the user be logged on when using the webservice/wcf/ajax, which is currently done via cookie/session id but as far as I am aware all the above methods allow this.

On the whole whichever method I end up using will be accessing the database via standardised accessible method calls, so I'm not tied into keeping everything within the same project/namespace.

Would love to hear peoples thoughts / experiences on this!

+2  A: 

I would recommend doing it as controller actions. Here's why: You may end up being able to make the same action return several different representations. For example, a GetPerson() action could be able to return Person data as XML, JSON, or HTML (and possibly other representations).

This is a common approach in Rails and also in ASPMVC.

Charlie Flowers
Definitely this method, with the controller action either rendering partials (user controls in asp.net mvc language), or json, depending on your requirements. Easy and completely painless.
Michael
A: 

You should definitely use Controller actions, here are a couple of reasons why:

  • Using WCF/Webservices would just be overkill for doing this (My assumption is that you don't already have any WCF/Webservices code which everyone uses)
  • Using controller actions fits naturally in these kind of situations
  • You can have an easy fallback if the user has Javascript disabled or an unsupported browser. (By just creating a couple of action methods which return the complete view)
  • Also as Michael mentioned you can perform this atomic updates by rendering partials

Hope that helps.

Khaja Minhajuddin