最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript work before submitting ASP.NET MVC form - Stack Overflow

programmeradmin1浏览0评论

I have an ASP.NET 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 result I get 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.

I have an ASP.NET 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 result I get 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.

Share Improve this question edited May 7, 2011 at 9:08 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Oct 13, 2010 at 21:44 Rasmus ChristensenRasmus Christensen 8,53113 gold badges54 silver badges82 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 14

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');
   }
})

The solution is add on submit event to form tag

onsubmit="return onSubmitClick(this);"
 function onSubmitClick(item) {

    console.log(item);

    return false;
}

it's work well. GL ! HF !

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.

发布评论

评论列表(0)

  1. 暂无评论