ASP.net mvc Call function on link click - asp.net-mvc

I want to be able to call a method from a link click. The situation is I have a chat client within my website. When the user clicks on the Open Chat client link I want to fire off a method that updates all the contacts the user is allowed to talk to and than it fires off javascript to open the chat client.
Ive got the javascript to open the chat client firing but not the method before. All the method will do is update some database records that the chat client accesses (I only want to do this if they open the chat client otherwise it is overkill and an unneeded perf hit).
Thanks in advance

The easiest solution is to use jQuery (or any other js library) to make an ajax call to your action method. Depending on how your app works, you can make the call asyncrhonously, or wait for your method to respond before opening the chat window.
If using jQuery, the only line of js needed is:
$.get('/controller/method', function(response) { alert('db updated!') });

Related

Open new window from GSP and forward to external URL

I need some help. I am converting a Struts application to Grails.
There is a particular action I am trying to convert.
There is a form in a jsp that asks a user to select a customer from a dropdown list and the user clicks one of two buttons.
When the user clicks on the "View Reports" button, an onclick event calls a javascript function that issues window.open on a STRUTS action class, passing the selected customer and selected action.
In the action class the http request has some attributes set (request.setAttribute (..)) and a forward is performed to an external application. The attributes that where set are used by the application for sign on. This is done as a POST.
My problem is I am not quite sure how to wire this flow using the Grails framework. I was able to get as far as the user selecting a customer, clicking an actionSubmit button, reading the selected customer from params, adding my attributes to 'request' and then..I am stuck.
How to open a new window? (Tried javascript way as was done with Struts).
Also I am able to issue a redirect to the external site in the controller, however a redirect is done as a GET and not a POST, as well as the redirect is done in the same window. Any help in laying this out would be awesome. Thanks
You can't redirect a user from server using POST.
I can see some possible solutions:
1 . If you don't need to pass through your server to validate or request some data, you can use this:
<form action="http://someotherserver.com" method="post">
2 . Create a controller that redirect to a page in your own site and in this page make a treatment that receives the paramters and then redirect the user to another domain using ajax.
In Grails, I've used createLink, with the 'base' attribute to do this.
<g:createLink base="${params.dynamicURL}">Link</g:createLink>
I think something like this would work, but you can research it here: http://grails.org/doc/latest/ref/Tags/createLink.html

jsTree to send a Ajax request to web-service when new node is added

I am using jsTree, and on adding a new item to the tree, i want to send an AJAX request to the server when a new item is added.
Here's what i found a link , on searching
I want the Ajax request to be sent to a vb.net web-service, this is done by using create.jstree event as shown in the link above.
But what will my web-service consist of? I want the web-service to know and save the changes(new node added, deleted or renamed).
Can anybody help me out on this?

Redirect to a remote URL with a POST verb and form data in MVC3

I currently have a small HTML form for a third party payment service, which comprises a flashy submit button and some hidden fields. These hidden field values are specific to the payment service, but the I wish to display the submit button as just one of many payment service options on a view based on a base class payment model, so I don't want to have specifics in my base payment model. Ideally I would like the submit button to call into an action in a service specific controller, and this action will gather service specific data, and submit a post request to the service's remote site, just as if I had clicked the submit button on the html form for that service. How can I achieve this?
If you want the form to be posted from the user's browser, you're going to have to use some client-side script either on a timer or on the Ready() event.
Can you do it server-side using HttpWebRequest?
i doubt that we can post data when redirecting. However what you want to achieve may be done using WebClient object from System.Net namespace. Not sure if it solves your problem but you can instantiate and call it like
WebClient client = new WebClient();
client.UploadString("http://www.xyz.com", "post", "a=b&c=d");

How to detect changes to a table using JQuery

I have a table which always updates by clicking on another link. This link has ajax functionality which is created by serverside API at run time and JQuery has no control over this link.
Now whenever this link changes table, I want to call a function by JQuery, how do I do that?

How can I make something similar to that cool bar on the top of stackoverflow that pops up when you get a new badge?

When you get a new badge on stackoverflow.com, a bar appears at the top of the screen informing you of your achievement. It sticks around until the user closes it.
I rather like that system of informing the user about new news related to the site or their account. It's fairly unintrusive, but still clearly communicates the information. Even if all users receive a notification this way, it sticks around for each user until they have acknowledged seeing it.
I'm running a system using Ruby on Rails on a PostGres database. What's the best way to implement a similar system on my setup?
Edit: Just to clarify, I'm interested both in the server side and client side of the setup.
The effect can be accomplished with jQuery and the slideDown method (http://api.jquery.com/slideDown/). Set an onClick event to make the element slideUp, hide, or hit an AJAX call to let you know that the user got the message and dismissed it.
You could set the contents of the element (I'd go with a div) via an AJAX call, or you could simply populate the div with the appropriate message when generating your page, start the div off as hidden, and then kick off the slideDown method when the page load is complete via a $(document).ready definition (http://think2loud.com/jquery-document-ready-howto/).
You could use something like the jquery popup bubble extension: http://www.farmcode.org/post/2009/04/06/jQuery-popup-bubble-extension.aspx

Resources