Fernando Medeiros
Fernando Medeiros

Reputation: 139

Action not receiving AJAX data

My action isn't receiving one of the AJAX parameters, do anyone know why?

AJAX

 $('#formLista').submit(function (e) {
            e.preventDefault();
            if ($(this).validate()) {
                $.ajax({
                    url: '@Url.Action("CriarListaAcesso")',
                    type: 'POST',
                    dataType: 'JSON',
                    data: $(this).serialize() + '&[email protected]',
                    success: function (obj) {
                        if (obj.success == true) {
                            alert('Acesso definido com sucesso');
                            $('#fecharAcLista').click();
                        }
                        else {
                            alert('### ocorreu um erro ao definir o acesso ###');
                        }

                    }
                }).always(function () {

                });

            }
        });

Action

//I receive the model of the form, but not the idLista string
public ActionResult CriarListaAcesso(UsuarioLista usrListas, string idLista)
{
}

the ViewBag.codLista have the string representing the id of the object.

Tried hardcoding the Id, still receiving null string

            $.ajax({
            url: '@Url.Action("CriarListaAcesso")',
            type: 'POST',
            data:  {usrListas: $(this).serialize() , idLista : '1234'},
            success: function (obj) {
                if (obj.success == true) {
                    alert('Acesso definido com sucesso');
                    $('#fecharAcLista').click();
                }
                else {
                    alert('### ocorreu um erro ao definir o acesso ###');
                }

            }
        });

Action null string image: http://gyazo.com/05bc1243600b62eab2a14b50da17af10

Upvotes: 1

Views: 115

Answers (3)

Fernando Medeiros
Fernando Medeiros

Reputation: 139

I created a workaround, passing the string on one of the properties of my model:

<input type="text" hidden name="codLista" value="@ViewBag.codLista">

so my ajax call look like this

$('#formLista').submit(function (e) {
        e.preventDefault();
        if ($(this).validate()) {
            $.ajax({
                url: '@Url.Action("CriarListaAcesso")',
                type: 'POST',
                dataType: 'JSON',
                data: $(this).serialize(),
                success: function (obj) {
                    if (obj.success == true) {
                        alert('Acesso definido com sucesso');
                        $('#fecharAcLista').click();
                    }
                    else {
                        alert('### ocorreu um erro ao definir o acesso ###');
                    }

                }
            }).always(function () {

            });

        }
    });

Upvotes: 1

stackada
stackada

Reputation: 414

u can create new array element and append to serialized data

var data = $(this).serializeArray();
    data.push({name: 'idLista', value: @ViewBag.codLista});

or turn your form type from post to get

Upvotes: 0

user786
user786

Reputation: 4374

You can do something like this

  var v=$("form").serialize();
  v=v.replace("=",":").replace("&",",");

And then wrap it in json brackets

 data:{v},...

Hope this helps

Upvotes: 0

Related Questions