1

I have in my MVC view..

 //Submit file
 <% using (Html.BeginForm("MethodName","ControllerName", FormMethod.Post, new { enctype = "multipart/form-data"})) { %>         
     <input type="file" name="file" id="file"/>
     <input type="text" id="file-name" name="Id"/>
     <input type="submit" value="Submit" name="Submit"/>
 <% } %>     

//Save link.
 <span class="Update" onclick="js.function()">Update</span>

Once I click on the Submit button, I go to controller and the method to run some code that submits the file to the database. After that is finished, I need to auto-click/force click the save link so that the js function is run after the submit .. how can I do that.

ZVenue
  • 4,967
  • 16
  • 61
  • 92

3 Answers3

2

I would use Ajax.BeginForm and in the OnSuccess handler you can click the button. Your code would look something like this (not tested but should lead you in the right direction):

<% using (Ajax.BeginForm("MethodName","ControllerName",
    new AjaxOptions { 
        OnSuccess ="OnSuccess",
        OnFailure ="OnFailure" 
    },  
    new { 
        enctype = "multipart/form-data"
    })) { %>         
     <input type="file" name="file" id="file"/>
     <input type="text" id="file-name" name="Id"/>
     <input type="submit" value="Submit" name="Submit"/>
 <% } %>    

Javascript:

function OnSuccess() {
    $('.Update').trigger('click');
}

Edit: If you were not trying to upload a file my above answer would be what you want. However, I have looked into this a little more as I noticed you were trying to post a file; and you cannot upload files using ajax without using html 5, iframe hacks, etc. It appears to me that your best bet would be to use an uploader such uploadify or plupload that resolve this issue using html5, flash, or hacks based on how you set them up.

Also, binding-httppostedfilebase-using-ajax-beginform is a similar question that has something that may be of help.

Community
  • 1
  • 1
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
0

You can do it a number of ways.. You can use the submit button as your trigger, prevent the default from happening with it which would be the standard method of posting. Then invoke a jquery post/get/ajax method instead. From which upon succession of that post/get/ajax run whatever else you want to do.

You could just click the button with jquery if you have something else in mind ie: $('button').trigger('click');

unfortunately the description you have is a little open ended, so its hard to give a specific answer.

chris
  • 36,115
  • 52
  • 143
  • 252
0

In order to automatically trigger a click (or any other event like this, for that matter), you need to first have the event established like so:

// setting up the event (in case they actually click it)
$('.Update').on('click', function () {
       test();
});

// this will automatically call it
$('.Update').trigger('click');

// whatever your function is doing
function test () {
    alert('here');
}

jsFiddle DEMO

Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96