Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 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" />

With
$("#SaveAndReturn").click(function () {
        $("#ReturnToList").val(true);
    });


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 () {
        $("#ReturnToList").val(true);
        document.getElementById("HelpdeskView").submit();
    });

But this doesn't work.

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

Thanks
Posted
Comments
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.

Thanks,
Have you tried to debug the code using FireBug of firefox ?

1 solution

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
JavaScript
$("#HelpdeskView").submit()


probably you'll have to write your form that way

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


or you could do the this way

C#
@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
 
Share this answer
 
v3
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900