views:

24

answers:

2

Hi

I guess this is a pretty easy one :). I have an aspnet mvc view with a Beginform, that specifies an action and controller to hit on submit. But on submit I want to call a service using jquery to get some data and then submit those data with the form.

Currently I have a submit button on the form where the onclick event of the button calls the javascript method. depending of what I get of result from the method I want the form to be submitted to the specified action.

Now I can't get this to work, is it the right way to do this or should I instead make a post using jquery? I think it would be nice to use what I have already specified as action/controller in the form

A: 

I think the best way is to use event 'submit' on form, because users can want to submit form by pressing Enter in some fields. Guess, it is possible to change some input values during this event,

jQuery('form#myform').submit(function(e){
   //....
   if (somevar == false)
   {
      // stop submitting form
      e.preventDefault();
   }
   else
   {
      jQuery('input#hiddeninput').val('somevalue');
   }
})
Aliaksei Shytkin
great answer, but not exactly what I want to do. I want to start a ajax request for some data, and only on respons submit/redirect the form. Using the example above would result in the request not completing before the form is submitted.
Rasmus Christensen
A: 

I think it would be better to perform the task of querying an external web service in a controller action. This way you would always submit the form to the same controller action which will query the service and based on the results would either render a view or redirect to some other action.

Darin Dimitrov