如何从在JavaScript剃刀Model对象获取JSON对象 [英] How to get JSON object from Razor Model object in javascript
本文介绍了如何从在JavaScript剃刀Model对象获取JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在视图模型对象,下面是属性:
In viewmodel object, below is the property:
public IList<CollegeInformationDTO> CollegeInformationlist { get; set; }
在VIEW,JavaScript是如下:
In VIEW, javascript is as follow:
var obj = JSON.stringify('@Model.CollegeInformationlist');
alert(obj[1].State); //NOT WORKING, giving string char
$.each('@Model.CollegeInformationlist', function (i, item) {
var obj = JSON.stringify(item);
var r = $.parseJSON(obj);
alert(r.State); //just giving undefined.
});
请指导在这里,我怎么能得到JSON对象的JavaScript。
Please guide here, how i can get JSON object in javascript.
推荐答案
您可以使用下列内容:
var json = @Html.Raw(Json.Encode(@Model.CollegeInformationlist));
这将输出以下(没有看到你的模型,我只包括一个字段):
This would output the following (without seeing your model I've only included one field):
<script>
var json = [{"State":"a state"}];
</script>
这篇关于如何从在JavaScript剃刀Model对象获取JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文