jquery - JavaScript - Submit Form - To a specific method on a controller -
i've got mvc 4 app. have following javascript function fires whenever change made element id of 'file'. submit works fine. however, submit specific action method on current controller , not method defined in html.beginform definition. i'm new , not sure how go doing this.
how can change code submit named action method on controller?
$(function () { $("#file").change(function () { $("form").submit(); }); });
you can set action before submitting form:
$(function () { $("#file").change(function () { $("form").attr("action", '@url.action("action", "controller")'); $("form").submit(); }); });
update: based on clearer understanding of requirements, if want user stay on same page when posting via change event, can make ajax post, this:
$(function () { $("#file").change(function () { var ajaxdata = { // initialise ajax data pass post target url }; $.ajax({ url: '@url.action("action", "controller")', // post target url goes here type: 'post', data: ajaxdata, success: function (data, text) { }, error: function(request, status, error) { } }); }); });
there further options available ajax calls in jquery docs.
Comments
Post a Comment