Solved

Submit all Razor forms on the page via Javascript/jQuery

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now