Solved

Submit all Razor forms on the page via Javascript/jQuery

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

690 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