问题描述
在我看来,我有以下代码,其中载有来自对象的属性,这些代码在我运行项目时显示得很好.但是,如何在Jquery中访问该对象的属性?目前,我从console.logs获取的内容是不确定的.
In my view I have the following code which is loaded with properties from an object, which display just fine when I run my project..But, how can I access the properties of that object in Jquery? Currently what I am getting from the console.logs is undefined.
我尝试将@ Html.Raw(Json.Encode(Model)添加到我的视图中,当我在html中点击检阅时,现在可以看到对象的每个道具,但是我仍然无法在jquery中访问em
I tried adding @Html.Raw(Json.Encode(Model) to my view and I can now see every prop of the object when I hit inspect in the html, but I still can´t access em in jquery.
function EditTrabajoEnColegio(li) {
console.log(li); //this displays the entire li in the console.
//From here, I get all as undefined
console.log(this.TrabajoId);
console.log(li.contrato);
console.log(li.mesIngreso);
console.log(li.anioIngreso);
console.log(li.esTitular);
console.log(li.cargoColegio);
console.log(li.sueldoBruto);
}
<li id="li-@Model.TrabajoId" class="list-group-item"
onclick="EditTrabajoEnColegio(this)">
@Model.MesIngresoStJohns/@Model.AnioIngresoStJohns
-
@(Model.AnioEgresoStJohns != null ?
Model.MesEgresoStJohns.ToString() + "/" +
Model.AnioEgresoStJohns.ToString() : "Actualidad")
-
@Model.SedeNombre
Model.TipoDocenteId
Model.TieneContrato
@Model.CargoColegio
@Html.HiddenFor(x => x.TrabajoId)
@Html.HiddenFor(x => x.SedeId)
@Html.HiddenFor(x => x.AnioEgresoStJohns)
@Html.HiddenFor(x => x.AnioIngresoStJohns)
@Html.HiddenFor(x => x.MesIngresoStJohns)
@Html.HiddenFor(x => x.MesEgresoStJohns)
@Html.HiddenFor(x => x.FechaIngreso)
@Html.HiddenFor(x => x.FechaEgreso)
@Html.HiddenFor(x => x.SedeNombre)
@Html.HiddenFor(x => x.CargoColegio)
@Html.HiddenFor(x => x.TipoDocenteId)
@Html.HiddenFor(x => x.EsTitular)
@Html.HiddenFor(x => x.TieneContrato)
@Html.HiddenFor(x => x.SueldoBruto)
</li>
}
任何帮助将不胜枚举.
推荐答案
对于以后的读者来说,我能够解决它.
To future readers, I was able to resolve it.
我所做的是
这使我可以传递定义的对象
That allowed me to pass the defined object
在jquery中
function EditTrabajoEnColegio(li) {
$("#CargoColegio").val(li.CargoColegio);
$("#SueldoBruto").val(li.SueldoBruto);
$("#AnioIngresoStJohns").val(li.AnioIngresoStJohns);
$("#MesIngresoStJohns").val(li.MesIngresoStJohns);
}
请注意,参数名称在jquery中并不重要,它只是您从视图中获得的.
Note that the name of the parameter does not matter in jquery, it´s just something that you get from the view.
这篇关于从Razor视图中的对象获取值到Javascript/Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!