[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Submit all Razor forms on the page via Javascript/jQuery

Posted on 2016-07-23
2
Medium Priority
?
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Camillia earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…
Suggested Courses

650 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