JQuery presents form and its model to the controller

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

+2


source to share


3 answers


If you want to pass your complex model directly to a controller method from jQuery, take a look at this question .



+2


source


Create your controller action with FormCollection declared. Then just call UpdateModel which will map your form properties to your object.

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

    try
    {
        UpdateModel(a);
        ...

      

Your kind



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

      

And here is 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
        }
    );
}

      

+4


source


Do you want you to send additional information along with the information on the form?

If so, I would just dynamically create the elements <input type="hidden">

and add them to the form before submitting.

0


source







All Articles