我在客户端(本例中为浏览器)中有一些json对象,如下所示
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");
})
如您所见,我通过jquery post方法将它发送到我自己的开发机器上的IISexpress服务器。
在C#中我创建了相应的模型:
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; }
}
我也设置了相应的控制器动作
[Route("api/reglas")]
[HttpPost]
public HttpResponseMessage postRegla(reglaInsercion laRegla)
{
return Request.CreateResponse(HttpStatusCode.OK);
}
但是当我调试我的代码时,laRegla对象的alll成员为null或零,具体取决于数据类型。我错过了什么?我已经阅读了文档,但我找不到我做错的事。
答案 0 :(得分:1)
您需要按如下方式定义模型:
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; }
}
将public
密钥添加到您的媒体资源。
答案 1 :(得分:0)
我可能错了,但我认为你需要以下方法通过AJAX调用你的方法。
[System.Web.Services.WebMethod]
[Route("api/reglas")]
[HttpPost]
public HttpResponseMessage postRegla(reglaInsercion laRegla)
{
return Request.CreateResponse(HttpStatusCode.OK);
}