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?

6
  • 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. Commented Aug 18, 2015 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. Commented Aug 18, 2015 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? Commented Aug 18, 2015 at 19:54
  • @AndyWiesendanger that's correct. The first button works as expected as it's just a basic type="submit". Commented Aug 18, 2015 at 19:56
  • stackoverflow.com/questions/442704/… Commented Aug 18, 2015 at 19:58

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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") {...}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.