Arrays @在MVC4中通过json迭代的for循环

Arrays @在MVC4中通过json迭代的for循环,arrays,json,asp.net-mvc,asp.net-mvc-4,display-templates,Arrays,Json,Asp.net Mvc,Asp.net Mvc 4,Display Templates,(已编辑)现在我已修复了以前的错误,但得到了错误: 路径中存在非法字符 我试图遍历一些json,构建一个无序的链接列表。我在显示模板中有@for语句,在视图中有Html.DisplayFor。这是我的密码: 控制器: using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.Data; using System.IO; using

(已编辑)现在我已修复了以前的错误,但得到了错误:

路径中存在非法字符

我试图遍历一些json,构建一个无序的链接列表。我在显示模板中有
@for
语句,在视图中有
Html.DisplayFor
。这是我的密码:

控制器:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace app.Web.Areas.Admin.Controllers
{
    public class CommonReportsController : Controller
    {
        public ActionResult CommonReports(Guid orgId)
        {
            JObject TopListData = JObject.Parse(System.IO.File.ReadAllText(@"C:\Dev\app\app.Web\json\TopListData.json"));
            string cData = Newtonsoft.Json.JsonConvert.SerializeObject(TopListData);
            var TopListDataView = TopListData;
            return View(TopListDataView);
        }

    }
}
视图:

@model app.API.Models.CommonReports.CommonReports模型
@使用app.API.Resources;
@使用app.Web.Utility;
@{
ViewBag.Title=Text.Get(Text.eTextType.Label,@Text.Get(Text.eTextType.Label,“CommonReports_Title”);
}
@面包屑节{
@(Text.Get(Text.eTextType.Tab,@Text.Get(Text.eTextType.Label,“CommonReports_Title”))
}
@视图包。标题
@DisplayFor(m=>m.CommonReports,“CommonReportsDisplayTemplate”)
显示模板:

@model app.API.Models.CommonReports.CommonReportsModel
@using app.API.Resources;
<ul class="row-centered-list">
@for (var i = 0; i < 1; i++)
{
<li><img src="~/Images/document.png" />&nbsp;&nbsp;@Html.ActionLink(@Text.Get(Text.eTextType.Label, TopListDataView[i].ListLabel), TopListDataView[i].ListView, TopListDataView[i].ListController, null, new { data_toggle = "tooltip", data_original_title = TopListDataView[i].ListToolTip })<span class="newRed"> - @Text.Get(Text.eTextType.Label, "CommonReports_New")</span></li>
}
</ul><br />
@model app.API.Models.CommonReports.CommonReports模型
@使用app.API.Resources;
    @对于(变量i=0;i<1;i++) {
  • @Html.ActionLink(@Text.Get(Text.eTextType.Label,TopListDataView[i].ListLabel),TopListDataView[i].ListView,TopListDataView[i].ListController,null,新建{data\u toggle=“tooltip”,data\u original\u title=TopListDataView[i].ListToolTip})-@Text.Get(Text.eTextType.Label,“CommonReports\u new”)
  • }

型号:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace InRollPlus.API.Models.CommonReports
{
public class CommonReportsModel
{
    public class TopListData
    {
        public string ListLabel
        {
            get;
            set;
        }
        public string ListView
        {
            get;
            set;
        }
        public string ListController
        {
            get;
            set;
        }
        public string ListToolTip
        {
            get;
            set;
        }
    }
    [UIHint("CommonReportsDisplayTemplate")]
    public string CommonReports
    {
        get;
        set;
    }

    public string TopListView
    {
        get;
        set;
    }
    public IList<TopListData> TopListDataView
    {
        get;
        set;
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用系统文本;
使用System.Web.Mvc;
命名空间InRollPlus.API.Models.CommonReports
{
公共类CommonReportsModel
{
公共类TopListData
{
公共字符串列表标签
{
得到;
设置
}
公共字符串列表视图
{
得到;
设置
}
公共字符串列表控制器
{
得到;
设置
}
公共字符串列表工具提示
{
得到;
设置
}
}
[UIHint(“CommonReportsDisplayTemplate”)]
公共字符串报告
{
得到;
设置
}
公共字符串TopListView
{
得到;
设置
}
公共IList TopListDataView
{
得到;
设置
}
}
}
我做错了什么?

在控制器上尝试代码

 public ActionResult CommonReports(Guid orgId)
  { 
     var jsonReader = new StreamReader(Server.MapPath("C:\Dev\app\app.Web\json\TopListData.json"));//Your Json File Path
     var rawJson = jsonReader.ReadToEnd();
     jsonReader.Close();
     var TopListDataView = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TopListData>>(rawJson);//Get List Of all Data Json File And TopListData Name As Model Class
     return View(TopListDataView);
   }
公共操作结果CommonReports(Guid orgId) { var jsonReader=newstreamreader(Server.MapPath(“C:\Dev\app\app.Web\json\TopListData.json”);//您的json文件路径 var rawJson=jsonReader.ReadToEnd(); jsonReader.Close(); var TopListDataView=Newtonsoft.Json.JsonConvert.DeserializeObject(rawJson);//获取所有数据Json文件的列表和TopListData名称作为模型类 返回视图(TopListDataView); }
因此,事实证明,不需要处理Json。我将获取.net对象,因此无需解决此问题。

模型.TopListDataView[I].ListLabel
TopListDataView
字符串类型,因此也没有意义。无法理解您真正想要做什么。TopListDataView是一个json数组。那么我应该如何引用它呢?没有任何意义-在您的控制器中,
字符串cData-…
的意义是什么-您从来没有使用过它(当您使用
返回视图(TopListDataView);
时,它就被扔掉了;
它也可能是
返回视图(TopListData);
)cData实际上正在获取TopListData并反序列化它。我只是忘了这样使用它:
var-TopListDataView=cData。请为您的视图提供一个字符串列表(链接),而不是作为JSON提供。然后使用foreach循环创建链接列表。。
 public ActionResult CommonReports(Guid orgId)
  { 
     var jsonReader = new StreamReader(Server.MapPath("C:\Dev\app\app.Web\json\TopListData.json"));//Your Json File Path
     var rawJson = jsonReader.ReadToEnd();
     jsonReader.Close();
     var TopListDataView = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TopListData>>(rawJson);//Get List Of all Data Json File And TopListData Name As Model Class
     return View(TopListDataView);
   }