Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript MVC
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 9-Apr-13 6:35am
Comments
Tadit Dash at 9-Apr-13 11:49am
   
Have you added the jQuery files to your project ?
Paul Unsy at 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,
Tadit Dash at 10-Apr-13 1:50am
   
Have you tried to debug the code using FireBug of firefox ?

1 solution

Rate this: bad
good
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
 $("#HelpdeskView").submit()
 
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
  Permalink  
v3
Comments
Paul Unsy at 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
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2014
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