POST in Ajax arrives with null data in the Actionresult of MVC5

Asked

Viewed 77 times

0

I have an AJAX function that is the event click of a button where I pick up all the checkbox which are checked and caught the values of each and play on an array this way:

$("#btnDropMessageSents").click(function (e) {

    var ids = [];

    $("#inbox-table input[type=checkbox]:checked").each(function () {
        if (this.checked === true) {
            ids.push($(this).val());
        }
    }); 


    if (ids.length > 0)
    {
        $.ajax({
            url: '/Inbox/DeleteDefinitive',
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: values = [{ 'values': '1006' }, { 'values': '1005' }, { 'values': '1004' }],
            dataType: "json",
            traditional: true,
            success: function (data, status, xhr) {
                alert(data);
            },
            error: function (data) {
                console.log(data);
            }
        });
    }
})

On the line data: values I put values in my hand to see if it would work anyway it was not.

Already mine ActionResult in controller is like this:

[HttpPost]
public ActionResult DeleteDefinitive(string[] values)
{
    var MensagemEnviadaDomain = new MensagemEnviada();
    foreach (var item in values)
    {
        var Model = MensagemEnviadaDomain.GetItem(_ => _.COD_MENSAGEM == Convert.ToInt32(item));
        MensagemEnviadaDomain.Edit(new MensagemEnviadaDto()
        {
            COD_MENSAGEM = Model.COD_MENSAGEM,
            COD_AUTOR = Model.COD_AUTOR,
            COD_REMETENTE = Model.COD_REMETENTE,
            ID = Model.ID,
            STATUS_AUTOR = Model.STATUS_AUTOR,
            STATUS_REMETENTE = "E",
        });
    }

    return (RedirectToAction("Sent", "Inbox"));
}

I have tried many times and different ways in my role in AJAX I do not know what can be but the mistake that gives is always the same :

Error 500 Internal Server Error

but this error is due to my array of values empty arrives or as null in action... How to fix this?

  • Hi, Rodrigo, please check to see if the title describes exactly the problem, saying "problem with X" could be the title of the 30 thousand questions of the site :) The ideal is to describe briefly and exactly what it is about. Formatting code with the Stack Snippets is only for HTML+CSS+JS that can be executed, otherwise use simple formatting: button { } of the editor.

2 answers

0

This way of passing the date for ajax function could be provoking, for stamping on the undefine island.

Try it this way

data: {'values':[{ 'values': '1006' }, { 'values': '1005' }, { 'values': '1004' }]},

inserir a descrição da imagem aqui

0

I make it a little simpler:

[HttpPost]
public ActionResult DeleteDefinitive(List<string> dataList)
{
    return Json(new
    {
        success = true,
        count = dataList.Count(),
        items = string.Join(",", dataList)
    });
}

my View:

@{
    ViewBag.Title = "StackOverflow";
}

<form>
    <ul>
        @for (var i = 1000; i < 1010; i++)
        {
            <li>
                <label for="chk_@i">
                    <input type="checkbox" id="chk_@i" name="chk" value="@i"/> @i
                </label>
            </li>
        }
    </ul>
    <a href="#" class="button">Submit</a>
</form>

@section scripts{
    <script>
        $(".button").click(function () {

            var url = '@Url.Action("DeleteDefinitive", "Home")',
                dta = [];

            $("input[name=chk][type=checkbox]:checked").each(function () {
                dta.push($(this).val());
            });

            $.post(url, { dataList: dta }, function (data) {
                alert("total: " + data.count + "\n values: " + data.items);
            });
        });
    </script>
}

outworking:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.