Retrieving a view using Ajax
The first step is to split my view into a number of partial views and introduce placeholders for the details and comments that I load once a widget is selected.
I now want to be able to update the list of widgets via Ajax. Because I put the list into a partial view I simply need to add another action on my controller for retrieving this partial view.
The last step is to call this action with JQuery and update the DOM.
I can load the details and comments in a similar fashion.
Which is really very simple. Now let’s allow the user to add widgets.
Form submission using Ajax
To allow adding widgets I created a simple form to handle the data entry. The only tricky bit is that I want to submit this form using Ajax. I’m going to use the built-in MVC Ajax libraries to submit the form.
It’s possible to do this with JQuery but I find it much easier with the MVC Ajax libraries. Now we simply need to refresh the list of widgets in the OnComplete method (using the routing I showed earlier) and we’re done.