Solved

Submit all Razor forms on the page via Javascript/jQuery

Posted on 2016-07-23
2
45 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

840 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