Click here to Skip to main content
13,051,413 members (51,195 online)
Rate this:
Please Sign up or sign in to vote.
Hi Guys.

I have an MVC form where the user has the option to plain Save (using submit) or save and return where the form saves and they move on.

To achieve this I want to fire some jscript when the second button is clicked, it sets a value in the model to true, then I want it to then submit.

I tried the following
<input type="submit" value="Save & Return to Menu" name="SaveAndReturn" id="SaveAndReturn" />

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

But this simply submits presumably because the jscript doesn't get fired as the submit occurs first.

My MVC form starts with the following and I believe the button should be like the following:
@using (Html.BeginForm("HelpdeskView", "Helpdesk", @Model))
       <input type="button" value="Save & Return to Menu" name="SaveAndReturn" id="SaveAndReturn" />

But I don't know what to do about jscript. (I'm new to this...)

I have tried:
$("#SaveAndReturn").click(function () {

But this doesn't work.

Please help... I'm sure it's easy, but I'm struggling...

Posted 9-Apr-13 5:35am
Tadit Dash 9-Apr-13 11:49am
Have you added the jQuery files to your project ?
Paul Unsy 9-Apr-13 12:17pm
Yes. I'm using jQuery and javascript all over the show... I can fire the click event for the button, but I can't figure out the syntax to actually perform the submit.

Tadit Dash 10-Apr-13 1:50am
Have you tried to debug the code using FireBug of firefox ?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can fire the form submit event with jQuery using:
considering that your form has the id "HelpdeskView", please check in you generated html if the form has this id

probably you'll have to write your form that way

@using(Html.BeginForm("HelpdeskView", "Helpdesk", FormMethod.Post, new{id="HelpdeskView"}))

or you could do the this way

@using (Html.BeginForm("HelpdeskView", "Helpdesk", @Model))
       <input type="submit" value="Save & Return to Menu" name="SaveAndReturn" id="SaveAndReturn" />

then in your click method you just change the value of the $("#ReturnToList")

the click event will be called first so the form will be submited

hope it helps and sorry, my english is bad
Paul Unsy 10-Apr-13 12:54pm
Thank you Moykn

I did it with @using(Html.BeginForm("HelpdeskView", "Helpdesk", FormMethod.Post, new{id="HelpdeskView"})).

You are a genius and you saved my sanity. And for the record, your English is spot on.

Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100