我在访问我的视图中正在访问ViewBag
数据使用JQuery
。它工作正常,如下所示。如何访问jquery中的@ Html.Raw对象的数据
var theData = '@Html.Raw(Json.Encode(ViewBag.DealDetails))';
console.log("ss ", theData);
现在我想访问这些数据例如:月,价格。我怎样才能访问这些。请帮助你。
我在访问我的视图中正在访问ViewBag
数据使用JQuery
。它工作正常,如下所示。如何访问jquery中的@ Html.Raw对象的数据
var theData = '@Html.Raw(Json.Encode(ViewBag.DealDetails))';
console.log("ss ", theData);
现在我想访问这些数据例如:月,价格。我怎样才能访问这些。请帮助你。
如果您有对象数组。通过阵列和接入环路使用object.key
小例子,这里提供
var data = [{price:23,month:2,other:"sfdsdf"},{price:11,month:12,other:"hello"},{price:212,month:1,other:"hello"}];
for(var i=0;i<data.length; i++){
console.log("Price "+data[i].price);
console.log("Month "+data[i].month);
}
你找回对象的数组,这样你就可以像这样访问:
var month = theData[0].Month; // Month of first item
或者如果你想循环所有项目:
for (var i = 0; i < theData.length; i++) {
alert(theData[i].Month);
//Do something
}
编辑
您可以将数据传递这样代替:
<script>
var theData = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(ViewBag.DealDetail))
<script>
它表示'undefined'。我检查了'theData'的类型,所以它是'string' –
@brian查看我的编辑。确保'ViewBag.DealDetail'不是服务器端的字符串,但它们是对象。 – CodingYoshi
不一样。事实并非如此。 –
它说'undefined'。我检查了'theData'的类型,所以它是'string' –