5

When submitting one form from view, how can i also read or have controller read data in another form in same page?

1
  • 1
    Is it within a single <FORM> tag? Commented Jan 20, 2009 at 1:33

4 Answers 4

10

When you submit a form with a browser, it will only send data for the fields within that <form></form>. This is true regardless of the back-end technology that you use, whether it be ASP.net, MVC.net, PHP, Python, etc.

The only two options I can really think of would be:

  1. Do like WebForms does and just place a <form> around the entire page, and sort out the results later based on what button is pressed.
  2. Use Javascript/AJAX to gather any data you like, and push it up any way you like. You could even do some of this in real-time (like when a check box is checked) and not cause a postback of the page.

Of course, there's pros and cons to each, but that's the nature of the beast.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your share. it helped me get a handle on how I was going to handle data entry with asp.net mvc
2

You could do it on the client-side with a combination of Ajax and Javascript...

<SCRIPT language="JavaScript">
function submitforms()
{
        new Ajax.Request(formUrl,
        {
            parameters: $H({param1:value,param2:value}).toQueryString(),
            method: 'post',
            onSuccess: function(transport) {
               document.myform.submit();
            }
        }
}
</SCRIPT> 

Comments

1

You can't. If you have information on the page you want to submit, you must include it with the submitted form. You can use JavaScript to copy the information from one form to the next when submitting, though.

Comments

0
        var formData1 = $("#form1").serializeObject();
        var formData2 = $("#form2").serializeObject();
        $.extend(formData1, formData2);
        var formData = JSON.stringify(formData1);

        $.ajax({
            type: "POST",
            url: "@Url.Action("MyAction", "MyController")",
            data: formData,
            dataType: "json",
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                ...Do something with the data
            },
            error: function(result) {
                ...Handle the error
            }
        });

Then on your controller side (I'm using MVC but WebAPI would probably work the same) you can declare two separate parameters that match your client-side models and everything will be sorted out for you, that is assuming you don't have overlapping property names! Gotta love it when the magic happens!

public ActionResult MyAction(FormDataModel1 formData1, FormDataModel2 formData2)

Credit to https://github.com/macek/jquery-serialize-object for the serializeObject code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.