Taking the content of a JS variable in an Asp.net C#

Asked

Viewed 975 times

1

I have a Function that takes the values of my inputs, and I want to take the values of the variable for my method to perform the Insert. Does anyone have any tips on how to do this?

function insert_veiculo() {
  var placa = $("#placa").val();
  var quilometragem = $("#quilometragem").val();
  var cor = $("#cor").val();
  var tipo = $("#tipo").val();
  var chassi = $("#chassi").val();
  var ano = $("#ano").val();
  var modelo = $("#modelo").val();
}

At the top Function, I take all the values of my inputs

1 answer

2

You can use the $.getJSON to do what you want:

$.getJSON("/nomeTeuController/nomeTuaFuncao", { placa : placa, quilometragem : quilometragem, cor: cor, tipo : tipo, chassi : chassi, ano : ano, modelo :modelo }
   , function (result) {
       alert("Dados Gravados");
   });

And on your controller:

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult nomeTuaFuncao(string placa, decimal quilometragem, string cor, string tipo, string chassi, int ano, string modelo)
{
    try
    {
        var novoElemento = new Veiculo();
        novoElemento.placa = placa;
        novoElemento.quilometragem = quilometragem;
        novoElemento.cor = cor;
        novoElemento.tipo = tipo;
        novoElemento.chassi = chassi;
        novoElemento.ano = ano;
        novoElemento.modelo = modelo;
        db.Veiculo.add(novoElemento);
        db.SaveChanges();

        return Json("Dados Gravados", JsonRequestBehavior.AllowGet);
    }catch (Exception ex)
    {
        return Json(new { Result = "Erro", Message = ex.Message }, JsonRequestBehavior.AllowGet);
    }
}
  • And how do I handle this data in my dal? My DAL: http://i.imgur.com/Qetspnn.png

  • What is in the given DAL? An external database? It always depends on the database that is, and how to insert the data...

  • My idea would be, receive in my method the values of the variables JS and la, send to my DAL.

  • But this is a different issue than published... To receive the values you already have the solution

  • I apologize for my amateurism. It’s because I’m an app for college, and is being my cousin contact with Asp.net and c# and I came across this little "problem".

  • No problem. As I told you, you already have the solution to pass the values to the controller, now to write to your DAL will depend on the database database you use...

Show 2 more comments

Browser other questions tagged

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