2

I would like to submit a form using jQuery and submit it to a controller action for processing, to include all the model properties, is this possible?

Coppermill
  • 6,676
  • 14
  • 67
  • 92

3 Answers3

4

Create your Controller Action with the FormCollection declared. Then just call UpdateModel which will map your forms properties to your object.

public ActionResult MyAction(FormCollection form)
{
    MyDomainObject a = //possibly get from repository

    try
    {
        UpdateModel(a);
        ...

Your View

<form id='my-form' action='post' method='/MyController/MyAction'>
    //form elements
</form>

And here's some javascript.

$(document).ready(function()
{
    var f = $('my-form');
    var action = f.attr('action');
    var serializedForm = f.serialize();
    $.post
    (
        action,
        serializedForm,
        function()
        {
            //anything after the form submit
        }
    );
}
David
  • 15,150
  • 15
  • 61
  • 83
  • Note that you don't have to use `FormCollection` with this - this can also be an MVC model or viewmodel, though I used `.serializeArray()` instead of `.serialize()` when I did this. Also used `$.post(method, serializedForm, function() {} );` instead of `action, serializedForm` shown here. – vapcguy Mar 24 '15 at 23:23
2

If you'd like to pass your own complex model directly into the controller method from jQuery, check out this question.

Community
  • 1
  • 1
MrDustpan
  • 5,508
  • 6
  • 34
  • 39
0

Do you mean you want to send additional informational along with the information in the form?

If so, I would just dynamically create <input type="hidden"> elements and append them to the form before submitting.

Tinister
  • 11,097
  • 6
  • 35
  • 36