Call the controller via javascript or ajax and insert into the asp.net mvc table

0

How can I get an ul li item to pass its value (id) to my controller and insert it into the table?

I already have to list this code snippet below already works I can add items to the list and see text and value:

            $("#addPeca").click(function () {
            //Váriavel para checar se já existe na lista
            var jaExisteNaLista = false;
            $(".listasDasPecas").show();

            $('#ListPeca li').each(function () {
                haveSomeLi = true;
                var current = $(this).text();
                if (current == $("#Pecas option:selected").text()) {
                    jaExisteNaLista = true;
                }
            });

            if (!jaExisteNaLista) {
                $("#ListPeca").append("<li>" + $("#Pecas option:selected").text() + "<input type='checkbox' name='chkPeca' id='chkPeca' class='chkPeca' checked='checked' value='" + $("#Pecas option:selected").val() + "'></li>");
            } else {
                alert("Peca Já inserida!");
            }
        });

Now I want to take what comes from this li and when I click on add already calls my controller "addPeca" and makes the insert in the table according to id that veioda li. so I've done my controller like this until now, but I'm kind of lost:

    [HttpPost, ActionName("Detalhes")]
public ActionResult AddPecas(int id)
{

    using (Db db = new Db())
    {

        var lstPeca = Request.Form["chkPeca"];

        if (!string.IsNullOrEmpty(lstPeca))
        {
            //cria array de peças vindo do form
            int[] splTags = lstPeca.Split(',').Select(Int32.Parse).ToArray();

            if (splTags.Count() > 0)
            {
                //verifica id
                var PostPeca = db.Pecas.Where(p => splTags.Contains(p.Id));

                ConsertoDetalhes consDetalhe = new ConsertoDetalhes();

                // Add para ConsertoDetalhe
                foreach (var item in PostPeca)
                {
                    consDetalhe.ConsertoId = consertoId;
                    consDetalhe.ClienteId = clientId;
                    consDetalhe.PecasId = item.Id;//pecaID
                    consDetalhe.FuncionarioId = funcId;
                    consDetalhe.ValorTotal = item.ValorUnitatio;

                    db.ConsertoDetalhes.Add(consDetalhe);
                    db.SaveChanges();
                }

            }
        }

    }

    return View();
}
    
asked by anonymous 03.02.2018 / 19:27

1 answer

0

Look, I do not think this is possible! It could be inserted into SQL tables and files. But in ASP.NET table, I do not know ...

    
04.02.2018 / 04:45