1

I have some json object in the client (browser in this case) as follows

objeto = {
  idSala: idSala,
  listaEtapas: listaEtapas,
  listaMacros: listaMacros,
  listaTI: listaTI,
  listaTU: listaTU,
  listaUnidades: listaUnidades,
  listaTorres: listaTorres,
  valor: valor,
  regla: regla,
  finicio: finicio,
  ffin: ffin,
  activo: activo
};
$.post("/api/reglas", objeto).done(function() {
  alert("ok");
})

As you see I'm sending it thru jquery post method to a IISexpress server on my own developing machine.

In C# I've created the corresponding model:

public class reglaInsercion
{
      int idSala { get; set; }
      int[] listaMacros { get; set; }
      int[] listaEtapas { get; set; }
      int[] listaTI { get; set; }
      int[] listaTU { get; set; }
      int[] listaUnidades { get; set; }
      string [] listaTorres { get; set; }
      double valor { get; set; }
      string regla { get; set; }
      DateTime finicio { get; set; }
      DateTime ffin { get; set; }
      bool activo { get; set; }
}

And I also have set the respective controller action

[Route("api/reglas")]
[HttpPost]
public HttpResponseMessage postRegla(reglaInsercion laRegla)
{
    return Request.CreateResponse(HttpStatusCode.OK);
}

But when I debug my code the laRegla object has alll its members to null or zero depending of the data type. What am I missing? I've read the docs and I can't find what I'm doing wrong.

1
  • In Chrome console: Uncaught ReferenceError: idSala is not defined. Commented Feb 4, 2016 at 22:35

2 Answers 2

1

You need to define your model as follows:

public class reglaInsercion
{
    public int idSala { get; set; }
    public int[] listaMacros { get; set; }
    public int[] listaEtapas { get; set; }
    public int[] listaTI { get; set; }
    public int[] listaTU { get; set; }
    public int[] listaUnidades { get; set; }
    public string[] listaTorres { get; set; }
    public double valor { get; set; }
    public string regla { get; set; }
    public DateTime finicio { get; set; }
    public DateTime ffin { get; set; }
    public bool activo { get; set; }
}

Adding public key to your properties.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked!!! After four hours of trying almost everything. Thanks a lot. I feel very dumb right now
You're welcome friend, do not forget to accept this answer as a valid response, it may be useful to other users. Cheers. :)
0

I could be wrong, but I think you need the following above the methods your calling via AJAX.

[System.Web.Services.WebMethod]
[Route("api/reglas")]
[HttpPost]
public HttpResponseMessage postRegla(reglaInsercion laRegla)
{
    return Request.CreateResponse(HttpStatusCode.OK);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.