0
How can I get an item from ul li pass its value(id) to my controller and insert that value into the table?
I already have the list this chunk of code below already works I can add items in 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 read and when you click add already calls my controller "addPeca" and makes the Insert in the table according to id that I read. so I’ve done my controller like this so far, 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();
}
Just to clarify you will send a list of ids to der registered in the base, or will, send only one id per click?
– Robson Silva