Solved

Submit all Razor forms on the page via Javascript/jQuery

Posted on 2016-07-23
2
39 Views
Last Modified: 2016-07-25
I am using C# MVC with Razor.
I have a form tied to a model in a partial view that is rendered twice on a page, each with different model contents. They have each their save submit button:
<input type="submit" class="btn btn-primary" value="Save" />

Open in new window

This works perfectly. When pushing the button, the respective model in the respective form gets posted to the server and the server updates the changes.

Now I want a button that submits both forms at the same time, and that is not a part of the partialview that these forms consist of.
I was thinking some sort JavaScript / jQuery based method that gets fired and handles the whole thing.

Any suggestions?
0
Comment
Question by:itnifl
2 Comments
 
LVL 7

Accepted Solution

by:
Camillia earned 500 total points
ID: 41725949
This is how I do it at work

1. Add click event for the button in document.ready
2. Get the form input values in jQuery
3. Pass to Controller using Ajax

I don't have my work code in front of me but this is roughly how the code looks like

1. If your button is btnSubmit and you have some input values ...for example...a textbox named txtName

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

   var nameVal = $("#txtName").val();

   //Ajax call to Controller her

    $.ajax({
                type: "POST",
                url: "/yourcontoller/youractionname,
                data:{ name: nameVal },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function() {....},
                error: function() {......}
            });


}

//in controller

[HttpPost]
        public ActionResult getName(string name)
        {

      }

Open in new window

0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 41727081
Yes, I guess that will work. I was hoping on some magic way to trigger all the submits at once so that I wouldn't have to write code to handle it myself :) but I guess I will have to step up to my laziness and get things done ;)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now