0

I have two submits, one to submit the form on page 1, the other to submit form on page 1 and redirect to form 2 on page 2.

<input type="submit" value="Submit and Add Expense" onclick="window.location.href='@Url.Action("Create", "Expense")';"/>

The issue is, when this secondary submit button is clicked it just submits the form just like the first submit button. It appears that the redirect @url.action is not firing. Thoughts?

tereško
  • 58,060
  • 25
  • 98
  • 150
Tony D.
  • 551
  • 1
  • 10
  • 26
  • Don't have it be a submit button. A submit button is designed to submit the form with no other interaction. You can `preventDefault` in your onclick event, but then you're not technically using a submit anymore anyway. – krillgar Aug 18 '15 at 19:38
  • I guess the better question should be, what would be the best way of submitting and redirecting in a situation like this? Looking at this more closely, it appears that I'm able to do one or the other. – Tony D. Aug 18 '15 at 19:44
  • So you want the 2nd button to post, like normal submit, but after that then redirect, whereas the first button just post like normal and keep on same page? – Andy Wiesendanger Aug 18 '15 at 19:54
  • @AndyWiesendanger that's correct. The first button works as expected as it's just a basic type="submit". – Tony D. Aug 18 '15 at 19:56
  • http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework – Andy Wiesendanger Aug 18 '15 at 19:58
  • ^ I'll give this a go. thanks – Tony D. Aug 18 '15 at 20:14

2 Answers2

1

The following code should do the trick.

<input type="submit" id="btnOne" value="One"/>

<input type="button" id="btnTwo" value="One"/>

<script>
    var sample = sample || {};
    sample.url = '@Url.Action("Create", "Expense")';
</script>

//you can move it to a separate file
<script>
$(function(){

    $("#btnTwo").click(function(){

        var form = $("form");
        form.submit();

         setTimeout(function() {


            window.location.href = sample.url;
        },100);


    });

});
</script>
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • thanks for the reply. this didn't actually work though. clicking the secondary button, form didn't submit nor did it redirect to the expense page. – Tony D. Aug 18 '15 at 20:13
0

I wound up going the easy route of assigning value property to the button, and then checking the button value in the controller.

// View

<input type="submit" value="Submit" />
<button name="button" value="SubmitAndExpense">Submit and Add Expense</button>

// Controller

[HttpPost]
public ActionResult Create(string button)
{
   if (button != "SubmitAndExpense") {...}
}
Tony D.
  • 551
  • 1
  • 10
  • 26