views:

83

answers:

2

For an MVC 2 app that relies on many partial views and almost exclusively uses Ajax for POSTs/GETs, what would be the best way to implement the setting, passing, retrieval and display (using a JavaScript modal) of these messages?

My forms all POST (by way of jQuery $.ajax) to actions that return partial views (html) that are used to update a in the "success:" part of the $.ajax function.

I was hoping for some sort of mechanism in the master view that could "listen" for any messages that any of these partial views might be "delivering"--through their ViewData, for instance.

Thanks.

Edit:

After lots more searching, I found similar people trying to achieve the same thing as me, but none of the questions had a good answer. This one states the question best.

A: 

You should take a look at the Dropbox web interface. Get yourself a free account and use the interface for 10 mins - copy, move, delete a file. The messaging and validation system is great. And it's largely ajax on their site too. Perfect example of the user experience you want it sounds like.

As for the technical implementation, i have been thinking about this for some time:
I'd use a broker system on the client for which all page ajax requests go through. This way all responses can be collated within a single function (/object, however you design it) and thus the responses too. It's obviously the responses that are key so that Error messages and Status messages can be handled in the same place and rendered to the user in a uniform way.

Server side it wouldn't be too hard. I'd suggest that you have a graceful degradation solution to those actions that return JSON or other ajax responses, in that they can function (and return Error/Status messages) without the client having JavaScript enabled.

cottsak
Yes that second link is a referral plug. But the new user benefits from the incentive as much as i do: ["Once your friend registers and installs Dropbox, you'll both get extra space."](https://www.dropbox.com/referrals)
cottsak
A: 

Your "master view" wouldn't be able to have a mechanism like what you're thinking of simply because you're asking it to be aware of potential data that could be given to it based on a possible user interaction.

What I think @cottsak is trying to say is to have delegate functions in your JS code that handle error and success events. Thus you can have 100 Ajax requests but only 2 functions actually handling the response. Within these functions you'll have to normalize the way you deal with the responses so that you don't have to write conditionals for specific forms for example. This might require normalizing your forms to have identical structures and base functionality and differ only on their input content.

For example I use "Wizards" in some of my sites that deal with modal forms:

<div class="Wizard">
  <form>
  <!-- Any and all possible content -->
  </form>
</div>

All my forms of course differ on the actual inputs they have, but they're all normalized in the sense that there's dedicated elements in them for messaging and such. Every single form however is controlled by the same JS with a few exceptions for special scenarios.

Alex
Thanks Alex. What you're describing is something similar to what I'll probably implement. I sure wish I had started at the beginning of the project, though...I'm looking at going back through ~25 partial views now.
PolishedTurd
You're welcome, and yeah you'll have to go through your work again, but in the long run you'll be much happier and you pave the way to expand in the future with additional functionality that follows an already established structure. It took me 2 re-writes to really grasp how to make my "wizards" properly, but it was worth it. If I wanted to expand right now I would spend 5-10 minutes on markup and be done.
Alex
If it's any help, you can view one of my sites which makes heavy use of the "wizards" at www.fthisjob.net. That site uses my "version 1.0" wizards, but I've since cleaned up and enhanced them further for a sister site of it which includes improved usability in the form of descriptions of inputs, visual queues for errors (row text color and input border turn red with an alert icon in between), and linking for related "wizards" between each other. Alas, "version 2.0" is not published so I can't demonstrate it to you.
Alex
I'll do that. Thanks again.
PolishedTurd